Assorted spelling fixes.
[wine.git] / dlls / ddraw / tests / ddraw7.c
blobec50fb8253cd1be96b9c3f571d2f27cddb2cebf9
1 /*
2 * Copyright 2006, 2012-2014 Stefan Dösinger for CodeWeavers
3 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define COBJMACROS
21 #include "wine/test.h"
22 #include <limits.h>
23 #include "d3d.h"
25 static HRESULT (WINAPI *pDirectDrawCreateEx)(GUID *guid, void **ddraw, REFIID iid, IUnknown *outer_unknown);
27 struct vec2
29 float x, y;
32 struct vec3
34 float x, y, z;
37 struct vec4
39 float x, y, z, w;
42 struct create_window_thread_param
44 HWND window;
45 HANDLE window_created;
46 HANDLE destroy_window;
47 HANDLE thread;
50 static BOOL compare_float(float f, float g, unsigned int ulps)
52 int x = *(int *)&f;
53 int y = *(int *)&g;
55 if (x < 0)
56 x = INT_MIN - x;
57 if (y < 0)
58 y = INT_MIN - y;
60 if (abs(x - y) > ulps)
61 return FALSE;
63 return TRUE;
66 static BOOL compare_vec3(struct vec3 *vec, float x, float y, float z, unsigned int ulps)
68 return compare_float(vec->x, x, ulps)
69 && compare_float(vec->y, y, ulps)
70 && compare_float(vec->z, z, ulps);
73 static BOOL compare_vec4(struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
75 return compare_float(vec->x, x, ulps)
76 && compare_float(vec->y, y, ulps)
77 && compare_float(vec->z, z, ulps)
78 && compare_float(vec->w, w, ulps);
81 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
83 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
84 c1 >>= 8; c2 >>= 8;
85 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
86 c1 >>= 8; c2 >>= 8;
87 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
88 c1 >>= 8; c2 >>= 8;
89 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
90 return TRUE;
93 static ULONG get_refcount(IUnknown *iface)
95 IUnknown_AddRef(iface);
96 return IUnknown_Release(iface);
99 static DWORD WINAPI create_window_thread_proc(void *param)
101 struct create_window_thread_param *p = param;
102 DWORD res;
103 BOOL ret;
105 p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
106 0, 0, 640, 480, 0, 0, 0, 0);
107 ret = SetEvent(p->window_created);
108 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
110 for (;;)
112 MSG msg;
114 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
115 DispatchMessageA(&msg);
116 res = WaitForSingleObject(p->destroy_window, 100);
117 if (res == WAIT_OBJECT_0)
118 break;
119 if (res != WAIT_TIMEOUT)
121 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
122 break;
126 DestroyWindow(p->window);
128 return 0;
131 static void create_window_thread(struct create_window_thread_param *p)
133 DWORD res, tid;
135 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
136 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
137 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
138 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
139 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
140 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
141 res = WaitForSingleObject(p->window_created, INFINITE);
142 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
145 static void destroy_window_thread(struct create_window_thread_param *p)
147 SetEvent(p->destroy_window);
148 WaitForSingleObject(p->thread, INFINITE);
149 CloseHandle(p->destroy_window);
150 CloseHandle(p->window_created);
151 CloseHandle(p->thread);
154 static IDirectDrawSurface7 *get_depth_stencil(IDirect3DDevice7 *device)
156 IDirectDrawSurface7 *rt, *ret;
157 DDSCAPS2 caps = {DDSCAPS_ZBUFFER, 0, 0, 0};
158 HRESULT hr;
160 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
161 ok(SUCCEEDED(hr), "Failed to get the render target, hr %#x.\n", hr);
162 hr = IDirectDrawSurface7_GetAttachedSurface(rt, &caps, &ret);
163 ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
164 IDirectDrawSurface7_Release(rt);
165 return ret;
168 static HRESULT set_display_mode(IDirectDraw7 *ddraw, DWORD width, DWORD height)
170 if (SUCCEEDED(IDirectDraw7_SetDisplayMode(ddraw, width, height, 32, 0, 0)))
171 return DD_OK;
172 return IDirectDraw7_SetDisplayMode(ddraw, width, height, 24, 0, 0);
175 static D3DCOLOR get_surface_color(IDirectDrawSurface7 *surface, UINT x, UINT y)
177 RECT rect = {x, y, x + 1, y + 1};
178 DDSURFACEDESC2 surface_desc;
179 D3DCOLOR color;
180 HRESULT hr;
182 memset(&surface_desc, 0, sizeof(surface_desc));
183 surface_desc.dwSize = sizeof(surface_desc);
185 hr = IDirectDrawSurface7_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY, NULL);
186 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
187 if (FAILED(hr))
188 return 0xdeadbeef;
190 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
192 hr = IDirectDrawSurface7_Unlock(surface, &rect);
193 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
195 return color;
198 static HRESULT CALLBACK enum_z_fmt(DDPIXELFORMAT *format, void *ctx)
200 DDPIXELFORMAT *z_fmt = ctx;
202 if (U1(*format).dwZBufferBitDepth > U1(*z_fmt).dwZBufferBitDepth)
203 *z_fmt = *format;
205 return DDENUMRET_OK;
208 static IDirectDraw7 *create_ddraw(void)
210 IDirectDraw7 *ddraw;
212 if (FAILED(pDirectDrawCreateEx(NULL, (void **)&ddraw, &IID_IDirectDraw7, NULL)))
213 return NULL;
215 return ddraw;
218 static HRESULT WINAPI enum_devtype_cb(char *desc_str, char *name, D3DDEVICEDESC7 *desc, void *ctx)
220 BOOL *hal_ok = ctx;
221 if (IsEqualGUID(&desc->deviceGUID, &IID_IDirect3DTnLHalDevice))
223 *hal_ok = TRUE;
224 return DDENUMRET_CANCEL;
226 return DDENUMRET_OK;
229 static IDirect3DDevice7 *create_device(HWND window, DWORD coop_level)
231 IDirectDrawSurface7 *surface, *ds;
232 IDirect3DDevice7 *device = NULL;
233 DDSURFACEDESC2 surface_desc;
234 DDPIXELFORMAT z_fmt;
235 IDirectDraw7 *ddraw;
236 IDirect3D7 *d3d7;
237 HRESULT hr;
238 BOOL hal_ok = FALSE;
239 const GUID *devtype = &IID_IDirect3DHALDevice;
241 if (!(ddraw = create_ddraw()))
242 return NULL;
244 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, coop_level);
245 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
247 memset(&surface_desc, 0, sizeof(surface_desc));
248 surface_desc.dwSize = sizeof(surface_desc);
249 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
250 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
251 surface_desc.dwWidth = 640;
252 surface_desc.dwHeight = 480;
254 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
255 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
257 if (coop_level & DDSCL_NORMAL)
259 IDirectDrawClipper *clipper;
261 hr = IDirectDraw7_CreateClipper(ddraw, 0, &clipper, NULL);
262 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
263 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
264 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
265 hr = IDirectDrawSurface7_SetClipper(surface, clipper);
266 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
267 IDirectDrawClipper_Release(clipper);
270 hr = IDirectDraw7_QueryInterface(ddraw, &IID_IDirect3D7, (void **)&d3d7);
271 IDirectDraw7_Release(ddraw);
272 if (FAILED(hr))
274 IDirectDrawSurface7_Release(surface);
275 return NULL;
278 hr = IDirect3D7_EnumDevices(d3d7, enum_devtype_cb, &hal_ok);
279 ok(SUCCEEDED(hr), "Failed to enumerate devices, hr %#x.\n", hr);
280 if (hal_ok) devtype = &IID_IDirect3DTnLHalDevice;
282 memset(&z_fmt, 0, sizeof(z_fmt));
283 hr = IDirect3D7_EnumZBufferFormats(d3d7, devtype, enum_z_fmt, &z_fmt);
284 if (FAILED(hr) || !z_fmt.dwSize)
286 IDirect3D7_Release(d3d7);
287 IDirectDrawSurface7_Release(surface);
288 return NULL;
291 memset(&surface_desc, 0, sizeof(surface_desc));
292 surface_desc.dwSize = sizeof(surface_desc);
293 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
294 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
295 U4(surface_desc).ddpfPixelFormat = z_fmt;
296 surface_desc.dwWidth = 640;
297 surface_desc.dwHeight = 480;
298 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &ds, NULL);
299 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
300 if (FAILED(hr))
302 IDirect3D7_Release(d3d7);
303 IDirectDrawSurface7_Release(surface);
304 return NULL;
307 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
308 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
309 IDirectDrawSurface7_Release(ds);
310 if (FAILED(hr))
312 IDirect3D7_Release(d3d7);
313 IDirectDrawSurface7_Release(surface);
314 return NULL;
317 hr = IDirect3D7_CreateDevice(d3d7, devtype, surface, &device);
318 IDirect3D7_Release(d3d7);
319 IDirectDrawSurface7_Release(surface);
320 if (FAILED(hr))
321 return NULL;
323 return device;
326 static const UINT *expect_messages;
328 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
330 if (expect_messages && message == *expect_messages)
331 ++expect_messages;
333 return DefWindowProcA(hwnd, message, wparam, lparam);
336 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
337 * interface. This prevents subsequent SetCooperativeLevel() calls on a
338 * different window from failing with DDERR_HWNDALREADYSET. */
339 static void fix_wndproc(HWND window, LONG_PTR proc)
341 IDirectDraw7 *ddraw;
342 HRESULT hr;
344 if (!(ddraw = create_ddraw()))
345 return;
347 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
348 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
349 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
350 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
351 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
353 IDirectDraw7_Release(ddraw);
356 static void test_process_vertices(void)
358 IDirect3DVertexBuffer7 *src_vb, *dst_vb1, *dst_vb2;
359 D3DVERTEXBUFFERDESC vb_desc;
360 IDirect3DDevice7 *device;
361 struct vec4 *dst_data;
362 struct vec3 *dst_data2;
363 struct vec3 *src_data;
364 IDirect3D7 *d3d7;
365 D3DVIEWPORT7 vp;
366 HWND window;
367 HRESULT hr;
369 static D3DMATRIX world =
371 0.0f, 1.0f, 0.0f, 0.0f,
372 1.0f, 0.0f, 0.0f, 0.0f,
373 0.0f, 0.0f, 0.0f, 1.0f,
374 0.0f, 1.0f, 1.0f, 1.0f,
376 static D3DMATRIX view =
378 2.0f, 0.0f, 0.0f, 0.0f,
379 0.0f, -1.0f, 0.0f, 0.0f,
380 0.0f, 0.0f, 1.0f, 0.0f,
381 0.0f, 0.0f, 0.0f, 3.0f,
383 static D3DMATRIX proj =
385 1.0f, 0.0f, 0.0f, 1.0f,
386 0.0f, 1.0f, 1.0f, 0.0f,
387 0.0f, 1.0f, 1.0f, 0.0f,
388 1.0f, 0.0f, 0.0f, 1.0f,
391 window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW,
392 0, 0, 640, 480, 0, 0, 0, 0);
393 if (!(device = create_device(window, DDSCL_NORMAL)))
395 skip("Failed to create a 3D device, skipping test.\n");
396 DestroyWindow(window);
397 return;
400 hr = IDirect3DDevice7_GetDirect3D(device, &d3d7);
401 ok(SUCCEEDED(hr), "Failed to get Direct3D7 interface, hr %#x.\n", hr);
403 memset(&vb_desc, 0, sizeof(vb_desc));
404 vb_desc.dwSize = sizeof(vb_desc);
405 vb_desc.dwFVF = D3DFVF_XYZ;
406 vb_desc.dwNumVertices = 4;
407 hr = IDirect3D7_CreateVertexBuffer(d3d7, &vb_desc, &src_vb, 0);
408 ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr);
410 hr = IDirect3DVertexBuffer7_Lock(src_vb, 0, (void **)&src_data, NULL);
411 ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr);
412 src_data[0].x = 0.0f;
413 src_data[0].y = 0.0f;
414 src_data[0].z = 0.0f;
415 src_data[1].x = 1.0f;
416 src_data[1].y = 1.0f;
417 src_data[1].z = 1.0f;
418 src_data[2].x = -1.0f;
419 src_data[2].y = -1.0f;
420 src_data[2].z = 0.5f;
421 src_data[3].x = 0.5f;
422 src_data[3].y = -0.5f;
423 src_data[3].z = 0.25f;
424 hr = IDirect3DVertexBuffer7_Unlock(src_vb);
425 ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr);
427 memset(&vb_desc, 0, sizeof(vb_desc));
428 vb_desc.dwSize = sizeof(vb_desc);
429 vb_desc.dwFVF = D3DFVF_XYZRHW;
430 vb_desc.dwNumVertices = 4;
431 /* MSDN says that the last parameter must be 0 - check that. */
432 hr = IDirect3D7_CreateVertexBuffer(d3d7, &vb_desc, &dst_vb1, 4);
433 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
435 memset(&vb_desc, 0, sizeof(vb_desc));
436 vb_desc.dwSize = sizeof(vb_desc);
437 vb_desc.dwFVF = D3DFVF_XYZ;
438 vb_desc.dwNumVertices = 5;
439 /* MSDN says that the last parameter must be 0 - check that. */
440 hr = IDirect3D7_CreateVertexBuffer(d3d7, &vb_desc, &dst_vb2, 12345678);
441 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
443 memset(&vp, 0, sizeof(vp));
444 vp.dwX = 64;
445 vp.dwY = 64;
446 vp.dwWidth = 128;
447 vp.dwHeight = 128;
448 vp.dvMinZ = 0.0f;
449 vp.dvMaxZ = 1.0f;
450 hr = IDirect3DDevice7_SetViewport(device, &vp);
451 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
453 hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb1, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0);
454 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
455 hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb2, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0);
456 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
458 hr = IDirect3DVertexBuffer7_Lock(dst_vb1, 0, (void **)&dst_data, NULL);
459 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
460 ok(compare_vec4(&dst_data[0], +1.280e+2f, +1.280e+2f, +0.000e+0f, +1.000e+0f, 4096),
461 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
462 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
463 ok(compare_vec4(&dst_data[1], +1.920e+2f, +6.400e+1f, +1.000e+0f, +1.000e+0f, 4096),
464 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
465 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
466 ok(compare_vec4(&dst_data[2], +6.400e+1f, +1.920e+2f, +5.000e-1f, +1.000e+0f, 4096),
467 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
468 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
469 ok(compare_vec4(&dst_data[3], +1.600e+2f, +1.600e+2f, +2.500e-1f, +1.000e+0f, 4096),
470 "Got unexpected vertex 3 {%.8e, %.8e, %.8e, %.8e}.\n",
471 dst_data[3].x, dst_data[3].y, dst_data[3].z, dst_data[3].w);
472 hr = IDirect3DVertexBuffer7_Unlock(dst_vb1);
473 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
475 hr = IDirect3DVertexBuffer7_Lock(dst_vb2, 0, (void **)&dst_data2, NULL);
476 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
477 /* Small thing without much practical meaning, but I stumbled upon it,
478 * so let's check for it: If the output vertex buffer has no RHW value,
479 * the RHW value of the last vertex is written into the next vertex. */
480 ok(compare_vec3(&dst_data2[4], +1.000e+0f, +0.000e+0f, +0.000e+0f, 4096),
481 "Got unexpected vertex 4 {%.8e, %.8e, %.8e}.\n",
482 dst_data2[4].x, dst_data2[4].y, dst_data2[4].z);
483 hr = IDirect3DVertexBuffer7_Unlock(dst_vb2);
484 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
486 /* Try a more complicated viewport, same vertices. */
487 memset(&vp, 0, sizeof(vp));
488 vp.dwX = 10;
489 vp.dwY = 5;
490 vp.dwWidth = 246;
491 vp.dwHeight = 130;
492 vp.dvMinZ = -2.0f;
493 vp.dvMaxZ = 4.0f;
494 hr = IDirect3DDevice7_SetViewport(device, &vp);
495 ok(SUCCEEDED(hr), "Failed to set viewport, hr %#x.\n", hr);
497 hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb1, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0);
498 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
500 hr = IDirect3DVertexBuffer7_Lock(dst_vb1, 0, (void **)&dst_data, NULL);
501 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
502 ok(compare_vec4(&dst_data[0], +1.330e+2f, +7.000e+1f, -2.000e+0f, +1.000e+0f, 4096),
503 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
504 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
505 ok(compare_vec4(&dst_data[1], +2.560e+2f, +5.000e+0f, +4.000e+0f, +1.000e+0f, 4096),
506 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
507 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
508 ok(compare_vec4(&dst_data[2], +1.000e+1f, +1.350e+2f, +1.000e+0f, +1.000e+0f, 4096),
509 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
510 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
511 ok(compare_vec4(&dst_data[3], +1.945e+2f, +1.025e+2f, -5.000e-1f, +1.000e+0f, 4096),
512 "Got unexpected vertex 3 {%.8e, %.8e, %.8e, %.8e}.\n",
513 dst_data[3].x, dst_data[3].y, dst_data[3].z, dst_data[3].w);
514 hr = IDirect3DVertexBuffer7_Unlock(dst_vb1);
515 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
517 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &world);
518 ok(SUCCEEDED(hr), "Failed to set world transform, hr %#x.\n", hr);
519 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &view);
520 ok(SUCCEEDED(hr), "Failed to set view transform, hr %#x.\n", hr);
521 hr = IDirect3DDevice7_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &proj);
522 ok(SUCCEEDED(hr), "Failed to set projection transform, hr %#x.\n", hr);
524 hr = IDirect3DVertexBuffer7_ProcessVertices(dst_vb1, D3DVOP_TRANSFORM, 0, 4, src_vb, 0, device, 0);
525 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
527 hr = IDirect3DVertexBuffer7_Lock(dst_vb1, 0, (void **)&dst_data, NULL);
528 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
529 ok(compare_vec4(&dst_data[0], +2.560e+2f, +7.000e+1f, -2.000e+0f, +3.333e-1f, 4096),
530 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
531 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
532 ok(compare_vec4(&dst_data[1], +2.560e+2f, +7.813e+1f, -2.750e+0f, +1.250e-1f, 4096),
533 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
534 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
535 ok(compare_vec4(&dst_data[2], +2.560e+2f, +4.400e+1f, +4.000e-1f, +4.000e-1f, 4096),
536 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
537 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
538 ok(compare_vec4(&dst_data[3], +2.560e+2f, +8.182e+1f, -3.091e+0f, +3.636e-1f, 4096),
539 "Got unexpected vertex 3 {%.8e, %.8e, %.8e, %.8e}.\n",
540 dst_data[3].x, dst_data[3].y, dst_data[3].z, dst_data[3].w);
541 hr = IDirect3DVertexBuffer7_Unlock(dst_vb1);
542 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
544 IDirect3DVertexBuffer7_Release(dst_vb2);
545 IDirect3DVertexBuffer7_Release(dst_vb1);
546 IDirect3DVertexBuffer7_Release(src_vb);
547 IDirect3D7_Release(d3d7);
548 IDirect3DDevice7_Release(device);
549 DestroyWindow(window);
552 static void test_coop_level_create_device_window(void)
554 HWND focus_window, device_window;
555 IDirectDraw7 *ddraw;
556 HRESULT hr;
558 focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
559 0, 0, 640, 480, 0, 0, 0, 0);
560 ddraw = create_ddraw();
561 ok(!!ddraw, "Failed to create a ddraw object.\n");
563 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
564 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
565 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
566 ok(!device_window, "Unexpected device window found.\n");
567 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
568 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
569 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
570 ok(!device_window, "Unexpected device window found.\n");
571 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
572 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
573 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
574 ok(!device_window, "Unexpected device window found.\n");
575 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
576 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
577 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
578 ok(!device_window, "Unexpected device window found.\n");
579 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
580 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
581 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
582 ok(!device_window, "Unexpected device window found.\n");
584 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
585 if (broken(hr == DDERR_INVALIDPARAMS))
587 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
588 IDirectDraw7_Release(ddraw);
589 DestroyWindow(focus_window);
590 return;
593 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
594 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
595 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
596 ok(!device_window, "Unexpected device window found.\n");
597 hr = IDirectDraw7_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
598 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
599 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
600 ok(!device_window, "Unexpected device window found.\n");
602 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
603 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
604 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
605 ok(!device_window, "Unexpected device window found.\n");
606 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
607 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
608 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
609 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
610 ok(!!device_window, "Device window not found.\n");
612 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
613 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
614 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
615 ok(!device_window, "Unexpected device window found.\n");
616 hr = IDirectDraw7_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
617 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
618 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
619 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
620 ok(!!device_window, "Device window not found.\n");
622 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
623 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
624 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
625 ok(!device_window, "Unexpected device window found.\n");
626 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
627 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
628 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
629 ok(!device_window, "Unexpected device window found.\n");
630 hr = IDirectDraw7_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
631 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
632 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
633 ok(!device_window, "Unexpected device window found.\n");
634 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
635 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
636 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
637 ok(!!device_window, "Device window not found.\n");
639 IDirectDraw7_Release(ddraw);
640 DestroyWindow(focus_window);
643 static void test_clipper_blt(void)
645 IDirectDrawSurface7 *src_surface, *dst_surface;
646 RECT client_rect, src_rect;
647 IDirectDrawClipper *clipper;
648 DDSURFACEDESC2 surface_desc;
649 unsigned int i, j, x, y;
650 IDirectDraw7 *ddraw;
651 RGNDATA *rgn_data;
652 D3DCOLOR color;
653 HRGN r1, r2;
654 HWND window;
655 DDBLTFX fx;
656 HRESULT hr;
657 DWORD *ptr;
658 DWORD ret;
660 static const DWORD src_data[] =
662 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
663 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
664 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
666 static const D3DCOLOR expected1[] =
668 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
669 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
670 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
671 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
673 /* Nvidia on Windows seems to have an off-by-one error
674 * when processing source rectangles. Our left = 1 and
675 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
676 * read as well, but only for the edge pixels on the
677 * output image. The bug happens on the y axis as well,
678 * but we only read one row there, and all source rows
679 * contain the same data. This bug is not dependent on
680 * the presence of a clipper. */
681 static const D3DCOLOR expected1_broken[] =
683 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
684 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
685 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
686 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
688 static const D3DCOLOR expected2[] =
690 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
691 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
692 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
693 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
696 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
697 10, 10, 640, 480, 0, 0, 0, 0);
698 ShowWindow(window, SW_SHOW);
699 ddraw = create_ddraw();
700 ok(!!ddraw, "Failed to create a ddraw object.\n");
702 ret = GetClientRect(window, &client_rect);
703 ok(ret, "Failed to get client rect.\n");
704 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
705 ok(ret, "Failed to map client rect.\n");
707 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
708 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
710 hr = IDirectDraw7_CreateClipper(ddraw, 0, &clipper, NULL);
711 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
712 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
713 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
714 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
715 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
716 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
717 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
718 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
719 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
720 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
721 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
722 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
723 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
724 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
725 "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
726 rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
727 rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
728 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
729 HeapFree(GetProcessHeap(), 0, rgn_data);
731 r1 = CreateRectRgn(0, 0, 320, 240);
732 ok(!!r1, "Failed to create region.\n");
733 r2 = CreateRectRgn(320, 240, 640, 480);
734 ok(!!r2, "Failed to create region.\n");
735 CombineRgn(r1, r1, r2, RGN_OR);
736 ret = GetRegionData(r1, 0, NULL);
737 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
738 ret = GetRegionData(r1, ret, rgn_data);
739 ok(!!ret, "Failed to get region data.\n");
741 DeleteObject(r2);
742 DeleteObject(r1);
744 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
745 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
746 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
747 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
748 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
749 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
751 HeapFree(GetProcessHeap(), 0, rgn_data);
753 memset(&surface_desc, 0, sizeof(surface_desc));
754 surface_desc.dwSize = sizeof(surface_desc);
755 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
756 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
757 surface_desc.dwWidth = 640;
758 surface_desc.dwHeight = 480;
759 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
760 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
761 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
762 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
763 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
764 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
766 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
767 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
768 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
769 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
771 memset(&fx, 0, sizeof(fx));
772 fx.dwSize = sizeof(fx);
773 hr = IDirectDrawSurface7_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
774 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
775 hr = IDirectDrawSurface7_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
776 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
778 hr = IDirectDrawSurface7_Lock(src_surface, NULL, &surface_desc, 0, NULL);
779 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
780 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
781 ptr = surface_desc.lpSurface;
782 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
783 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
784 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
785 hr = IDirectDrawSurface7_Unlock(src_surface, NULL);
786 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
788 hr = IDirectDrawSurface7_SetClipper(dst_surface, clipper);
789 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
791 SetRect(&src_rect, 1, 1, 5, 2);
792 hr = IDirectDrawSurface7_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
793 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
794 for (i = 0; i < 4; ++i)
796 for (j = 0; j < 4; ++j)
798 x = 80 * ((2 * j) + 1);
799 y = 60 * ((2 * i) + 1);
800 color = get_surface_color(dst_surface, x, y);
801 ok(compare_color(color, expected1[i * 4 + j], 1)
802 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
803 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
807 U5(fx).dwFillColor = 0xff0000ff;
808 hr = IDirectDrawSurface7_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
809 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
810 for (i = 0; i < 4; ++i)
812 for (j = 0; j < 4; ++j)
814 x = 80 * ((2 * j) + 1);
815 y = 60 * ((2 * i) + 1);
816 color = get_surface_color(dst_surface, x, y);
817 ok(compare_color(color, expected2[i * 4 + j], 1),
818 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
822 hr = IDirectDrawSurface7_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
823 ok(hr == DDERR_BLTFASTCANTCLIP, "Got unexpected hr %#x.\n", hr);
825 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
826 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
827 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
828 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
829 DestroyWindow(window);
830 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
831 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
832 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
833 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
834 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
835 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
836 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
837 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
838 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
839 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
840 hr = IDirectDrawSurface7_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
841 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
843 IDirectDrawSurface7_Release(dst_surface);
844 IDirectDrawSurface7_Release(src_surface);
845 IDirectDrawClipper_Release(clipper);
846 IDirectDraw7_Release(ddraw);
849 static void test_coop_level_d3d_state(void)
851 IDirectDrawSurface7 *rt, *surface;
852 IDirect3DDevice7 *device;
853 IDirectDraw7 *ddraw;
854 IDirect3D7 *d3d;
855 D3DCOLOR color;
856 DWORD value;
857 HWND window;
858 HRESULT hr;
860 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
861 0, 0, 640, 480, 0, 0, 0, 0);
862 if (!(device = create_device(window, DDSCL_NORMAL)))
864 skip("Failed to create a 3D device, skipping test.\n");
865 DestroyWindow(window);
866 return;
869 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
870 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
871 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
872 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
873 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
874 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
875 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
876 ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
877 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
878 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
879 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
880 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
881 color = get_surface_color(rt, 320, 240);
882 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
884 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
885 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
886 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
887 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
888 IDirect3D7_Release(d3d);
889 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
890 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
891 hr = IDirectDrawSurface7_IsLost(rt);
892 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
893 hr = IDirectDraw7_RestoreAllSurfaces(ddraw);
894 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
895 IDirectDraw7_Release(ddraw);
897 hr = IDirect3DDevice7_GetRenderTarget(device, &surface);
898 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
899 ok(surface == rt, "Got unexpected surface %p.\n", surface);
900 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
901 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
902 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
903 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
904 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
905 ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
906 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
907 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
908 color = get_surface_color(rt, 320, 240);
909 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
911 IDirectDrawSurface7_Release(surface);
912 IDirectDrawSurface7_Release(rt);
913 IDirect3DDevice7_Release(device);
914 DestroyWindow(window);
917 static void test_surface_interface_mismatch(void)
919 IDirectDraw7 *ddraw = NULL;
920 IDirect3D7 *d3d = NULL;
921 IDirectDrawSurface7 *surface = NULL, *ds;
922 IDirectDrawSurface3 *surface3 = NULL;
923 IDirect3DDevice7 *device = NULL;
924 DDSURFACEDESC2 surface_desc;
925 DDPIXELFORMAT z_fmt;
926 ULONG refcount;
927 HRESULT hr;
928 D3DCOLOR color;
929 HWND window;
931 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
932 0, 0, 640, 480, 0, 0, 0, 0);
933 ddraw = create_ddraw();
934 ok(!!ddraw, "Failed to create a ddraw object.\n");
935 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
936 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
938 memset(&surface_desc, 0, sizeof(surface_desc));
939 surface_desc.dwSize = sizeof(surface_desc);
940 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
941 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
942 surface_desc.dwWidth = 640;
943 surface_desc.dwHeight = 480;
945 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
946 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
948 hr = IDirectDrawSurface7_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
949 ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface3, hr %#x.\n", hr);
951 if (FAILED(IDirectDraw7_QueryInterface(ddraw, &IID_IDirect3D7, (void **)&d3d)))
953 skip("D3D interface is not available, skipping test.\n");
954 goto cleanup;
957 memset(&z_fmt, 0, sizeof(z_fmt));
958 hr = IDirect3D7_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
959 if (FAILED(hr) || !z_fmt.dwSize)
961 skip("No depth buffer formats available, skipping test.\n");
962 goto cleanup;
965 memset(&surface_desc, 0, sizeof(surface_desc));
966 surface_desc.dwSize = sizeof(surface_desc);
967 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
968 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
969 U4(surface_desc).ddpfPixelFormat = z_fmt;
970 surface_desc.dwWidth = 640;
971 surface_desc.dwHeight = 480;
972 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &ds, NULL);
973 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
974 if (FAILED(hr))
975 goto cleanup;
977 /* Using a different surface interface version still works */
978 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
979 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
980 refcount = IDirectDrawSurface7_Release(ds);
981 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
982 if (FAILED(hr))
983 goto cleanup;
985 /* Here too */
986 hr = IDirect3D7_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface7 *)surface3, &device);
987 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
988 if (FAILED(hr))
989 goto cleanup;
991 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
992 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
993 color = get_surface_color(surface, 320, 240);
994 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
996 cleanup:
997 if (surface3) IDirectDrawSurface3_Release(surface3);
998 if (surface) IDirectDrawSurface7_Release(surface);
999 if (device) IDirect3DDevice7_Release(device);
1000 if (d3d) IDirect3D7_Release(d3d);
1001 if (ddraw) IDirectDraw7_Release(ddraw);
1002 DestroyWindow(window);
1005 static void test_coop_level_threaded(void)
1007 struct create_window_thread_param p;
1008 IDirectDraw7 *ddraw;
1009 HRESULT hr;
1011 ddraw = create_ddraw();
1012 ok(!!ddraw, "Failed to create a ddraw object.\n");
1013 create_window_thread(&p);
1015 hr = IDirectDraw7_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1016 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1018 IDirectDraw7_Release(ddraw);
1019 destroy_window_thread(&p);
1022 static void test_depth_blit(void)
1024 IDirect3DDevice7 *device;
1025 static struct
1027 float x, y, z;
1028 DWORD color;
1030 quad1[] =
1032 { -1.0, 1.0, 0.50f, 0xff00ff00},
1033 { 1.0, 1.0, 0.50f, 0xff00ff00},
1034 { -1.0, -1.0, 0.50f, 0xff00ff00},
1035 { 1.0, -1.0, 0.50f, 0xff00ff00},
1037 static const D3DCOLOR expected_colors[4][4] =
1039 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1040 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1041 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1042 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1044 DDSURFACEDESC2 ddsd_new, ddsd_existing;
1046 IDirectDrawSurface7 *ds1, *ds2, *ds3, *rt;
1047 RECT src_rect, dst_rect;
1048 unsigned int i, j;
1049 D3DCOLOR color;
1050 HRESULT hr;
1051 IDirect3D7 *d3d;
1052 IDirectDraw7 *ddraw;
1053 DDBLTFX fx;
1054 HWND window;
1056 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1057 0, 0, 640, 480, 0, 0, 0, 0);
1058 if (!(device = create_device(window, DDSCL_NORMAL)))
1060 skip("Failed to create a 3D device, skipping test.\n");
1061 DestroyWindow(window);
1062 return;
1065 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
1066 ok(SUCCEEDED(hr), "Failed to get Direct3D7 interface, hr %#x.\n", hr);
1067 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
1068 ok(SUCCEEDED(hr), "Failed to get DirectDraw7 interface, hr %#x.\n", hr);
1069 IDirect3D7_Release(d3d);
1071 ds1 = get_depth_stencil(device);
1073 memset(&ddsd_new, 0, sizeof(ddsd_new));
1074 ddsd_new.dwSize = sizeof(ddsd_new);
1075 memset(&ddsd_existing, 0, sizeof(ddsd_existing));
1076 ddsd_existing.dwSize = sizeof(ddsd_existing);
1077 hr = IDirectDrawSurface7_GetSurfaceDesc(ds1, &ddsd_existing);
1078 ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1079 ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1080 ddsd_new.dwWidth = ddsd_existing.dwWidth;
1081 ddsd_new.dwHeight = ddsd_existing.dwHeight;
1082 U4(ddsd_new).ddpfPixelFormat = U4(ddsd_existing).ddpfPixelFormat;
1083 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
1084 ok(SUCCEEDED(hr), "Failed to create a z buffer, hr %#x.\n", hr);
1085 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
1086 ok(SUCCEEDED(hr), "Failed to create a z buffer, hr %#x.\n", hr);
1087 IDirectDraw7_Release(ddraw);
1089 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
1090 ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
1091 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
1092 ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
1093 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
1094 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
1096 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1097 ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
1099 /* Partial blit. */
1100 SetRect(&src_rect, 0, 0, 320, 240);
1101 SetRect(&dst_rect, 0, 0, 320, 240);
1102 hr = IDirectDrawSurface7_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1103 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1104 /* Different locations. */
1105 SetRect(&src_rect, 0, 0, 320, 240);
1106 SetRect(&dst_rect, 320, 240, 640, 480);
1107 hr = IDirectDrawSurface7_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1108 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1109 /* Streched. */
1110 SetRect(&src_rect, 0, 0, 320, 240);
1111 SetRect(&dst_rect, 0, 0, 640, 480);
1112 hr = IDirectDrawSurface7_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1113 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1114 /* Flipped. */
1115 SetRect(&src_rect, 0, 480, 640, 0);
1116 SetRect(&dst_rect, 0, 0, 640, 480);
1117 hr = IDirectDrawSurface7_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1118 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1119 SetRect(&src_rect, 0, 0, 640, 480);
1120 SetRect(&dst_rect, 0, 480, 640, 0);
1121 hr = IDirectDrawSurface7_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1122 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1123 /* Full, explicit. */
1124 SetRect(&src_rect, 0, 0, 640, 480);
1125 SetRect(&dst_rect, 0, 0, 640, 480);
1126 hr = IDirectDrawSurface7_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1127 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1128 /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1130 /* Depth blit inside a BeginScene / EndScene pair */
1131 hr = IDirect3DDevice7_BeginScene(device);
1132 ok(SUCCEEDED(hr), "Failed to start scene, hr %#x.\n", hr);
1133 /* From the current depth stencil */
1134 hr = IDirectDrawSurface7_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1135 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1136 /* To the current depth stencil */
1137 hr = IDirectDrawSurface7_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1138 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1139 /* Between unbound surfaces */
1140 hr = IDirectDrawSurface7_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1141 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1142 hr = IDirect3DDevice7_EndScene(device);
1143 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1145 /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1146 * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1147 * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1148 * a reliable result(z = 0.0) */
1149 memset(&fx, 0, sizeof(fx));
1150 fx.dwSize = sizeof(fx);
1151 hr = IDirectDrawSurface7_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1152 ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1154 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, 0xffff0000, 1.0f, 0);
1155 ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1156 SetRect(&dst_rect, 0, 0, 320, 240);
1157 hr = IDirectDrawSurface7_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1158 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1159 IDirectDrawSurface7_Release(ds3);
1160 IDirectDrawSurface7_Release(ds2);
1161 IDirectDrawSurface7_Release(ds1);
1163 hr = IDirect3DDevice7_BeginScene(device);
1164 ok(SUCCEEDED(hr), "Failed to start scene, hr %#x.\n", hr);
1165 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE,
1166 quad1, 4, 0);
1167 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1168 hr = IDirect3DDevice7_EndScene(device);
1169 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1171 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
1172 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1173 for (i = 0; i < 4; ++i)
1175 for (j = 0; j < 4; ++j)
1177 unsigned int x = 80 * ((2 * j) + 1);
1178 unsigned int y = 60 * ((2 * i) + 1);
1179 color = get_surface_color(rt, x, y);
1180 ok(compare_color(color, expected_colors[i][j], 1),
1181 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1185 IDirectDrawSurface7_Release(rt);
1186 IDirect3DDevice7_Release(device);
1187 DestroyWindow(window);
1190 static void test_texture_load_ckey(void)
1192 HWND window;
1193 IDirect3DDevice7 *device;
1194 IDirectDraw7 *ddraw;
1195 IDirectDrawSurface7 *src;
1196 IDirectDrawSurface7 *dst;
1197 DDSURFACEDESC2 ddsd;
1198 HRESULT hr;
1199 DDCOLORKEY ckey;
1200 IDirect3D7 *d3d;
1202 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1203 0, 0, 640, 480, 0, 0, 0, 0);
1204 if (!(device = create_device(window, DDSCL_NORMAL)))
1206 skip("Failed to create a 3D device, skipping test.\n");
1207 DestroyWindow(window);
1208 return;
1211 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
1212 ok(SUCCEEDED(hr), "Failed to get Direct3D7 interface, hr %#x.\n", hr);
1213 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
1214 ok(SUCCEEDED(hr), "Failed to get DirectDraw7 interface, hr %#x.\n", hr);
1215 IDirect3D7_Release(d3d);
1217 memset(&ddsd, 0, sizeof(ddsd));
1218 ddsd.dwSize = sizeof(ddsd);
1219 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1220 ddsd.dwHeight = 128;
1221 ddsd.dwWidth = 128;
1222 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1223 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &src, NULL);
1224 ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1225 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1226 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &dst, NULL);
1227 ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1229 /* No surface has a color key */
1230 hr = IDirect3DDevice7_Load(device, dst, NULL, src, NULL, 0);
1231 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1232 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1233 hr = IDirectDrawSurface7_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1234 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1235 ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1236 ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1238 /* Source surface has a color key */
1239 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1240 hr = IDirectDrawSurface7_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1241 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1242 hr = IDirect3DDevice7_Load(device, dst, NULL, src, NULL, 0);
1243 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1244 hr = IDirectDrawSurface7_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1245 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1246 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1247 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1249 /* Both surfaces have a color key: Dest ckey is overwritten */
1250 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1251 hr = IDirectDrawSurface7_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1252 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1253 hr = IDirect3DDevice7_Load(device, dst, NULL, src, NULL, 0);
1254 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1255 hr = IDirectDrawSurface7_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1256 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1257 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1258 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1260 /* Only the destination has a color key: It is deleted. This behavior differs from
1261 * IDirect3DTexture(2)::Load */
1262 hr = IDirectDrawSurface7_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1263 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1264 hr = IDirectDrawSurface7_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1265 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1266 hr = IDirect3DDevice7_Load(device, dst, NULL, src, NULL, 0);
1267 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1268 hr = IDirectDrawSurface7_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1269 todo_wine ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1271 IDirectDrawSurface7_Release(dst);
1272 IDirectDrawSurface7_Release(src);
1273 IDirectDraw7_Release(ddraw);
1274 IDirect3DDevice7_Release(device);
1275 DestroyWindow(window);
1278 static void test_zenable(void)
1280 static struct
1282 struct vec4 position;
1283 D3DCOLOR diffuse;
1285 tquad[] =
1287 {{ 0.0f, 480.0f, -0.5f, 1.0f}, 0xff00ff00},
1288 {{ 0.0f, 0.0f, -0.5f, 1.0f}, 0xff00ff00},
1289 {{640.0f, 480.0f, 1.5f, 1.0f}, 0xff00ff00},
1290 {{640.0f, 0.0f, 1.5f, 1.0f}, 0xff00ff00},
1292 IDirect3DDevice7 *device;
1293 IDirectDrawSurface7 *rt;
1294 D3DCOLOR color;
1295 HWND window;
1296 HRESULT hr;
1297 UINT x, y;
1298 UINT i, j;
1300 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1301 0, 0, 640, 480, 0, 0, 0, 0);
1302 if (!(device = create_device(window, DDSCL_NORMAL)))
1304 skip("Failed to create a 3D device, skipping test.\n");
1305 DestroyWindow(window);
1306 return;
1309 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1310 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
1312 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 0.0f, 0);
1313 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
1314 hr = IDirect3DDevice7_BeginScene(device);
1315 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1316 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, tquad, 4, 0);
1317 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1318 hr = IDirect3DDevice7_EndScene(device);
1319 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1321 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
1322 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1323 for (i = 0; i < 4; ++i)
1325 for (j = 0; j < 4; ++j)
1327 x = 80 * ((2 * j) + 1);
1328 y = 60 * ((2 * i) + 1);
1329 color = get_surface_color(rt, x, y);
1330 ok(compare_color(color, 0x0000ff00, 1),
1331 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1334 IDirectDrawSurface7_Release(rt);
1336 IDirect3DDevice7_Release(device);
1337 DestroyWindow(window);
1340 static void test_ck_rgba(void)
1342 static struct
1344 struct vec4 position;
1345 struct vec2 texcoord;
1347 tquad[] =
1349 {{ 0.0f, 480.0f, 0.25f, 1.0f}, {0.0f, 0.0f}},
1350 {{ 0.0f, 0.0f, 0.25f, 1.0f}, {0.0f, 1.0f}},
1351 {{640.0f, 480.0f, 0.25f, 1.0f}, {1.0f, 0.0f}},
1352 {{640.0f, 0.0f, 0.25f, 1.0f}, {1.0f, 1.0f}},
1353 {{ 0.0f, 480.0f, 0.75f, 1.0f}, {0.0f, 0.0f}},
1354 {{ 0.0f, 0.0f, 0.75f, 1.0f}, {0.0f, 1.0f}},
1355 {{640.0f, 480.0f, 0.75f, 1.0f}, {1.0f, 0.0f}},
1356 {{640.0f, 0.0f, 0.75f, 1.0f}, {1.0f, 1.0f}},
1358 static const struct
1360 D3DCOLOR fill_color;
1361 BOOL color_key;
1362 BOOL blend;
1363 D3DCOLOR result1;
1364 D3DCOLOR result2;
1366 tests[] =
1368 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x000000ff},
1369 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x000000ff},
1370 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00},
1371 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1372 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00807f00},
1373 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x0000ff00},
1374 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00},
1375 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1378 IDirectDrawSurface7 *texture;
1379 DDSURFACEDESC2 surface_desc;
1380 IDirect3DDevice7 *device;
1381 IDirectDrawSurface7 *rt;
1382 IDirectDraw7 *ddraw;
1383 IDirect3D7 *d3d;
1384 D3DCOLOR color;
1385 HWND window;
1386 DDBLTFX fx;
1387 HRESULT hr;
1388 UINT i;
1390 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1391 0, 0, 640, 480, 0, 0, 0, 0);
1392 if (!(device = create_device(window, DDSCL_NORMAL)))
1394 skip("Failed to create a 3D device, skipping test.\n");
1395 DestroyWindow(window);
1396 return;
1399 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
1400 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1401 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
1402 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1403 IDirect3D7_Release(d3d);
1405 memset(&surface_desc, 0, sizeof(surface_desc));
1406 surface_desc.dwSize = sizeof(surface_desc);
1407 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1408 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1409 surface_desc.dwWidth = 256;
1410 surface_desc.dwHeight = 256;
1411 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1412 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1413 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1414 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1415 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1416 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1417 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1418 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1419 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1420 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &texture, NULL);
1421 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1423 hr = IDirect3DDevice7_SetTexture(device, 0, texture);
1424 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1425 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1426 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1427 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1428 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1430 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
1431 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1433 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1435 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1436 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1437 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1438 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1440 memset(&fx, 0, sizeof(fx));
1441 fx.dwSize = sizeof(fx);
1442 U5(fx).dwFillColor = tests[i].fill_color;
1443 hr = IDirectDrawSurface7_Blt(texture, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1444 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1446 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 1.0f, 0);
1447 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
1448 hr = IDirect3DDevice7_BeginScene(device);
1449 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1450 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1451 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1452 hr = IDirect3DDevice7_EndScene(device);
1453 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1455 color = get_surface_color(rt, 320, 240);
1456 if (i == 2)
1457 todo_wine ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1458 tests[i].result1, i, color);
1459 else
1460 ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1461 tests[i].result1, i, color);
1463 U5(fx).dwFillColor = 0xff0000ff;
1464 hr = IDirectDrawSurface7_Blt(texture, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1465 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1467 hr = IDirect3DDevice7_BeginScene(device);
1468 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1469 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[4], 4, 0);
1470 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1471 hr = IDirect3DDevice7_EndScene(device);
1472 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1474 /* This tests that fragments that are masked out by the color key are
1475 * discarded, instead of just fully transparent. */
1476 color = get_surface_color(rt, 320, 240);
1477 if (i == 2)
1478 todo_wine ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1479 tests[i].result2, i, color);
1480 else
1481 ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1482 tests[i].result2, i, color);
1485 IDirectDrawSurface7_Release(rt);
1486 IDirectDrawSurface7_Release(texture);
1487 IDirectDraw7_Release(ddraw);
1488 IDirect3DDevice7_Release(device);
1489 DestroyWindow(window);
1492 static void test_ck_default(void)
1494 static struct
1496 struct vec4 position;
1497 struct vec2 texcoord;
1499 tquad[] =
1501 {{ 0.0f, 480.0f, 0.0f, 1.0f}, {0.0f, 0.0f}},
1502 {{ 0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
1503 {{640.0f, 480.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
1504 {{640.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
1506 IDirectDrawSurface7 *surface, *rt;
1507 DDSURFACEDESC2 surface_desc;
1508 IDirect3DDevice7 *device;
1509 IDirectDraw7 *ddraw;
1510 IDirect3D7 *d3d;
1511 D3DCOLOR color;
1512 DWORD value;
1513 HWND window;
1514 DDBLTFX fx;
1515 HRESULT hr;
1517 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1518 0, 0, 640, 480, 0, 0, 0, 0);
1520 if (!(device = create_device(window, DDSCL_NORMAL)))
1522 skip("Failed to create a 3D device, skipping test.\n");
1523 DestroyWindow(window);
1524 return;
1527 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
1528 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1529 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
1530 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1531 IDirect3D7_Release(d3d);
1533 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
1534 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1536 memset(&surface_desc, 0, sizeof(surface_desc));
1537 surface_desc.dwSize = sizeof(surface_desc);
1538 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1539 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1540 surface_desc.dwWidth = 256;
1541 surface_desc.dwHeight = 256;
1542 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1543 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
1544 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1545 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1546 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1547 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1548 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1549 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1550 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1551 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1552 hr = IDirect3DDevice7_SetTexture(device, 0, surface);
1553 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1555 memset(&fx, 0, sizeof(fx));
1556 fx.dwSize = sizeof(fx);
1557 U5(fx).dwFillColor = 0x000000ff;
1558 hr = IDirectDrawSurface7_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1559 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1561 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1562 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
1563 hr = IDirect3DDevice7_BeginScene(device);
1564 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1565 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1566 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1567 ok(!value, "Got unexpected color keying state %#x.\n", value);
1568 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1569 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1570 hr = IDirect3DDevice7_EndScene(device);
1571 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1572 color = get_surface_color(rt, 320, 240);
1573 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1575 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1576 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
1577 hr = IDirect3DDevice7_BeginScene(device);
1578 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1579 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1580 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1581 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1582 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1583 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1584 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1585 ok(!!value, "Got unexpected color keying state %#x.\n", value);
1586 hr = IDirect3DDevice7_EndScene(device);
1587 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1588 color = get_surface_color(rt, 320, 240);
1589 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1591 IDirectDrawSurface7_Release(surface);
1592 IDirectDrawSurface7_Release(rt);
1593 IDirect3DDevice7_Release(device);
1594 IDirectDraw7_Release(ddraw);
1595 DestroyWindow(window);
1598 static void test_ck_complex(void)
1600 IDirectDrawSurface7 *surface, *mipmap, *tmp;
1601 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, 0};
1602 DDSURFACEDESC2 surface_desc;
1603 IDirect3DDevice7 *device;
1604 DDCOLORKEY color_key;
1605 IDirectDraw7 *ddraw;
1606 IDirect3D7 *d3d;
1607 unsigned int i;
1608 ULONG refcount;
1609 HWND window;
1610 HRESULT hr;
1612 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1613 0, 0, 640, 480, 0, 0, 0, 0);
1614 if (!(device = create_device(window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1616 skip("Failed to create a 3D device, skipping test.\n");
1617 DestroyWindow(window);
1618 return;
1620 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
1621 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1622 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
1623 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1624 IDirect3D7_Release(d3d);
1626 memset(&surface_desc, 0, sizeof(surface_desc));
1627 surface_desc.dwSize = sizeof(surface_desc);
1628 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1629 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1630 surface_desc.dwWidth = 128;
1631 surface_desc.dwHeight = 128;
1632 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1633 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1635 hr = IDirectDrawSurface7_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1636 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1637 color_key.dwColorSpaceLowValue = 0x0000ff00;
1638 color_key.dwColorSpaceHighValue = 0x0000ff00;
1639 hr = IDirectDrawSurface7_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1640 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1641 memset(&color_key, 0, sizeof(color_key));
1642 hr = IDirectDrawSurface7_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1643 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1644 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1645 color_key.dwColorSpaceLowValue);
1646 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1647 color_key.dwColorSpaceHighValue);
1649 mipmap = surface;
1650 IDirectDrawSurface_AddRef(mipmap);
1651 for (i = 0; i < 7; ++i)
1653 hr = IDirectDrawSurface7_GetAttachedSurface(mipmap, &caps, &tmp);
1654 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
1655 hr = IDirectDrawSurface7_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1656 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1658 color_key.dwColorSpaceLowValue = 0x000000ff;
1659 color_key.dwColorSpaceHighValue = 0x000000ff;
1660 hr = IDirectDrawSurface7_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1661 ok(hr == DDERR_NOTONMIPMAPSUBLEVEL, "Got unexpected hr %#x, i %u.\n", hr, i);
1663 IDirectDrawSurface_Release(mipmap);
1664 mipmap = tmp;
1667 hr = IDirectDrawSurface7_GetAttachedSurface(mipmap, &caps, &tmp);
1668 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
1669 IDirectDrawSurface_Release(mipmap);
1670 refcount = IDirectDrawSurface7_Release(surface);
1671 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1673 memset(&surface_desc, 0, sizeof(surface_desc));
1674 surface_desc.dwSize = sizeof(surface_desc);
1675 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
1676 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1677 surface_desc.dwBackBufferCount = 1;
1678 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1679 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1681 hr = IDirectDrawSurface7_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1682 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1683 color_key.dwColorSpaceLowValue = 0x0000ff00;
1684 color_key.dwColorSpaceHighValue = 0x0000ff00;
1685 hr = IDirectDrawSurface7_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1686 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1687 memset(&color_key, 0, sizeof(color_key));
1688 hr = IDirectDrawSurface7_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1689 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1690 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1691 color_key.dwColorSpaceLowValue);
1692 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1693 color_key.dwColorSpaceHighValue);
1695 hr = IDirectDrawSurface7_GetAttachedSurface(surface, &caps, &tmp);
1696 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
1698 hr = IDirectDrawSurface7_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1699 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1700 color_key.dwColorSpaceLowValue = 0x0000ff00;
1701 color_key.dwColorSpaceHighValue = 0x0000ff00;
1702 hr = IDirectDrawSurface7_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1703 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1704 memset(&color_key, 0, sizeof(color_key));
1705 hr = IDirectDrawSurface7_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1706 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1707 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1708 color_key.dwColorSpaceLowValue);
1709 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1710 color_key.dwColorSpaceHighValue);
1712 IDirectDrawSurface_Release(tmp);
1714 refcount = IDirectDrawSurface7_Release(surface);
1715 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1716 IDirectDraw7_Release(ddraw);
1717 refcount = IDirect3DDevice7_Release(device);
1718 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1719 DestroyWindow(window);
1722 struct qi_test
1724 REFIID iid;
1725 REFIID refcount_iid;
1726 HRESULT hr;
1729 static void test_qi(const char *test_name, IUnknown *base_iface,
1730 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
1732 ULONG refcount, expected_refcount;
1733 IUnknown *iface1, *iface2;
1734 HRESULT hr;
1735 UINT i, j;
1737 for (i = 0; i < entry_count; ++i)
1739 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
1740 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
1741 if (SUCCEEDED(hr))
1743 for (j = 0; j < entry_count; ++j)
1745 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
1746 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
1747 if (SUCCEEDED(hr))
1749 expected_refcount = 0;
1750 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
1751 ++expected_refcount;
1752 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
1753 ++expected_refcount;
1754 refcount = IUnknown_Release(iface2);
1755 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
1756 refcount, test_name, i, j, expected_refcount);
1760 expected_refcount = 0;
1761 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
1762 ++expected_refcount;
1763 refcount = IUnknown_Release(iface1);
1764 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
1765 refcount, test_name, i, expected_refcount);
1770 static void test_surface_qi(void)
1772 static const struct qi_test tests[] =
1774 {&IID_IDirect3DTexture2, NULL, E_NOINTERFACE},
1775 {&IID_IDirect3DTexture, NULL, E_NOINTERFACE},
1776 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
1777 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
1778 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
1779 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
1780 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
1781 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
1782 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
1783 {&IID_IDirect3DDevice7, NULL, E_NOINTERFACE},
1784 {&IID_IDirect3DDevice3, NULL, E_NOINTERFACE},
1785 {&IID_IDirect3DDevice2, NULL, E_NOINTERFACE},
1786 {&IID_IDirect3DDevice, NULL, E_NOINTERFACE},
1787 {&IID_IDirect3DRampDevice, NULL, E_NOINTERFACE},
1788 {&IID_IDirect3DRGBDevice, NULL, E_NOINTERFACE},
1789 {&IID_IDirect3DHALDevice, NULL, E_NOINTERFACE},
1790 {&IID_IDirect3DMMXDevice, NULL, E_NOINTERFACE},
1791 {&IID_IDirect3DRefDevice, NULL, E_NOINTERFACE},
1792 {&IID_IDirect3DTnLHalDevice, NULL, E_NOINTERFACE},
1793 {&IID_IDirect3DNullDevice, NULL, E_NOINTERFACE},
1794 {&IID_IDirect3D7, NULL, E_NOINTERFACE},
1795 {&IID_IDirect3D3, NULL, E_NOINTERFACE},
1796 {&IID_IDirect3D2, NULL, E_NOINTERFACE},
1797 {&IID_IDirect3D, NULL, E_NOINTERFACE},
1798 {&IID_IDirectDraw7, NULL, E_NOINTERFACE},
1799 {&IID_IDirectDraw4, NULL, E_NOINTERFACE},
1800 {&IID_IDirectDraw3, NULL, E_NOINTERFACE},
1801 {&IID_IDirectDraw2, NULL, E_NOINTERFACE},
1802 {&IID_IDirectDraw, NULL, E_NOINTERFACE},
1803 {&IID_IDirect3DLight, NULL, E_NOINTERFACE},
1804 {&IID_IDirect3DMaterial, NULL, E_NOINTERFACE},
1805 {&IID_IDirect3DMaterial2, NULL, E_NOINTERFACE},
1806 {&IID_IDirect3DMaterial3, NULL, E_NOINTERFACE},
1807 {&IID_IDirect3DExecuteBuffer, NULL, E_NOINTERFACE},
1808 {&IID_IDirect3DViewport, NULL, E_NOINTERFACE},
1809 {&IID_IDirect3DViewport2, NULL, E_NOINTERFACE},
1810 {&IID_IDirect3DViewport3, NULL, E_NOINTERFACE},
1811 {&IID_IDirect3DVertexBuffer, NULL, E_NOINTERFACE},
1812 {&IID_IDirect3DVertexBuffer7, NULL, E_NOINTERFACE},
1813 {&IID_IDirectDrawPalette, NULL, E_NOINTERFACE},
1814 {&IID_IDirectDrawClipper, NULL, E_NOINTERFACE},
1815 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
1818 IDirectDrawSurface7 *surface;
1819 DDSURFACEDESC2 surface_desc;
1820 IDirect3DDevice7 *device;
1821 IDirectDraw7 *ddraw;
1822 HWND window;
1823 HRESULT hr;
1825 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1826 0, 0, 640, 480, 0, 0, 0, 0);
1827 /* Try to create a D3D device to see if the ddraw implementation supports
1828 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
1829 * doesn't support e.g. the IDirect3DTexture interfaces. */
1830 if (!(device = create_device(window, DDSCL_NORMAL)))
1832 skip("Failed to create a 3D device, skipping test.\n");
1833 DestroyWindow(window);
1834 return;
1836 IDirect3DDevice_Release(device);
1837 ddraw = create_ddraw();
1838 ok(!!ddraw, "Failed to create a ddraw object.\n");
1839 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1840 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1842 memset(&surface_desc, 0, sizeof(surface_desc));
1843 surface_desc.dwSize = sizeof(surface_desc);
1844 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1845 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1846 surface_desc.dwWidth = 512;
1847 surface_desc.dwHeight = 512;
1848 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1849 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1851 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface7, tests, sizeof(tests) / sizeof(*tests));
1853 IDirectDrawSurface7_Release(surface);
1854 IDirectDraw7_Release(ddraw);
1855 DestroyWindow(window);
1858 static void test_device_qi(void)
1860 static const struct qi_test tests[] =
1862 {&IID_IDirect3DTexture2, NULL, E_NOINTERFACE},
1863 {&IID_IDirect3DTexture, NULL, E_NOINTERFACE},
1864 {&IID_IDirectDrawGammaControl, NULL, E_NOINTERFACE},
1865 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
1866 {&IID_IDirectDrawSurface7, NULL, E_NOINTERFACE},
1867 {&IID_IDirectDrawSurface4, NULL, E_NOINTERFACE},
1868 {&IID_IDirectDrawSurface3, NULL, E_NOINTERFACE},
1869 {&IID_IDirectDrawSurface2, NULL, E_NOINTERFACE},
1870 {&IID_IDirectDrawSurface, NULL, E_NOINTERFACE},
1871 {&IID_IDirect3DDevice7, &IID_IDirect3DDevice7, S_OK },
1872 {&IID_IDirect3DDevice3, NULL, E_NOINTERFACE},
1873 {&IID_IDirect3DDevice2, NULL, E_NOINTERFACE},
1874 {&IID_IDirect3DDevice, NULL, E_NOINTERFACE},
1875 {&IID_IDirect3DRampDevice, NULL, E_NOINTERFACE},
1876 {&IID_IDirect3DRGBDevice, NULL, E_NOINTERFACE},
1877 {&IID_IDirect3DHALDevice, NULL, E_NOINTERFACE},
1878 {&IID_IDirect3DMMXDevice, NULL, E_NOINTERFACE},
1879 {&IID_IDirect3DRefDevice, NULL, E_NOINTERFACE},
1880 {&IID_IDirect3DTnLHalDevice, NULL, E_NOINTERFACE},
1881 {&IID_IDirect3DNullDevice, NULL, E_NOINTERFACE},
1882 {&IID_IDirect3D7, NULL, E_NOINTERFACE},
1883 {&IID_IDirect3D3, NULL, E_NOINTERFACE},
1884 {&IID_IDirect3D2, NULL, E_NOINTERFACE},
1885 {&IID_IDirect3D, NULL, E_NOINTERFACE},
1886 {&IID_IDirectDraw7, NULL, E_NOINTERFACE},
1887 {&IID_IDirectDraw4, NULL, E_NOINTERFACE},
1888 {&IID_IDirectDraw3, NULL, E_NOINTERFACE},
1889 {&IID_IDirectDraw2, NULL, E_NOINTERFACE},
1890 {&IID_IDirectDraw, NULL, E_NOINTERFACE},
1891 {&IID_IDirect3DLight, NULL, E_NOINTERFACE},
1892 {&IID_IDirect3DMaterial, NULL, E_NOINTERFACE},
1893 {&IID_IDirect3DMaterial2, NULL, E_NOINTERFACE},
1894 {&IID_IDirect3DMaterial3, NULL, E_NOINTERFACE},
1895 {&IID_IDirect3DExecuteBuffer, NULL, E_NOINTERFACE},
1896 {&IID_IDirect3DViewport, NULL, E_NOINTERFACE},
1897 {&IID_IDirect3DViewport2, NULL, E_NOINTERFACE},
1898 {&IID_IDirect3DViewport3, NULL, E_NOINTERFACE},
1899 {&IID_IDirect3DVertexBuffer, NULL, E_NOINTERFACE},
1900 {&IID_IDirect3DVertexBuffer7, NULL, E_NOINTERFACE},
1901 {&IID_IDirectDrawPalette, NULL, E_NOINTERFACE},
1902 {&IID_IDirectDrawClipper, NULL, E_NOINTERFACE},
1903 {&IID_IUnknown, &IID_IDirect3DDevice7, S_OK },
1906 IDirect3DDevice7 *device;
1907 HWND window;
1909 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1910 0, 0, 640, 480, 0, 0, 0, 0);
1911 if (!(device = create_device(window, DDSCL_NORMAL)))
1913 skip("Failed to create a 3D device, skipping test.\n");
1914 DestroyWindow(window);
1915 return;
1918 test_qi("device_qi", (IUnknown *)device, &IID_IDirect3DDevice7, tests, sizeof(tests) / sizeof(*tests));
1920 IDirect3DDevice7_Release(device);
1921 DestroyWindow(window);
1924 static void test_wndproc(void)
1926 LONG_PTR proc, ddraw_proc;
1927 IDirectDraw7 *ddraw;
1928 WNDCLASSA wc = {0};
1929 HWND window;
1930 HRESULT hr;
1931 ULONG ref;
1933 static const UINT messages[] =
1935 WM_WINDOWPOSCHANGING,
1936 WM_MOVE,
1937 WM_SIZE,
1938 WM_WINDOWPOSCHANGING,
1939 WM_ACTIVATE,
1940 WM_SETFOCUS,
1944 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
1945 ddraw = create_ddraw();
1946 ok(!!ddraw, "Failed to create a ddraw object.\n");
1948 wc.lpfnWndProc = test_proc;
1949 wc.lpszClassName = "ddraw_test_wndproc_wc";
1950 ok(RegisterClassA(&wc), "Failed to register window class.\n");
1952 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
1953 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1955 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1956 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1957 (LONG_PTR)test_proc, proc);
1958 expect_messages = messages;
1959 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1960 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1961 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
1962 expect_messages = NULL;
1963 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1964 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1965 (LONG_PTR)test_proc, proc);
1966 ref = IDirectDraw7_Release(ddraw);
1967 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1968 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1969 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1970 (LONG_PTR)test_proc, proc);
1972 /* DDSCL_NORMAL doesn't. */
1973 ddraw = create_ddraw();
1974 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1975 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1976 (LONG_PTR)test_proc, proc);
1977 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
1978 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1979 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1980 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1981 (LONG_PTR)test_proc, proc);
1982 ref = IDirectDraw7_Release(ddraw);
1983 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1984 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1985 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1986 (LONG_PTR)test_proc, proc);
1988 /* The original window proc is only restored by ddraw if the current
1989 * window proc matches the one ddraw set. This also affects switching
1990 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
1991 ddraw = create_ddraw();
1992 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1993 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1994 (LONG_PTR)test_proc, proc);
1995 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1996 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1997 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1998 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1999 (LONG_PTR)test_proc, proc);
2000 ddraw_proc = proc;
2001 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2002 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2003 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2004 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2005 (LONG_PTR)test_proc, proc);
2006 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2007 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2008 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2009 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2010 (LONG_PTR)test_proc, proc);
2011 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2012 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2013 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2014 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2015 (LONG_PTR)DefWindowProcA, proc);
2016 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2017 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2018 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
2019 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2020 (LONG_PTR)DefWindowProcA, proc);
2021 ref = IDirectDraw7_Release(ddraw);
2022 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2023 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2024 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2025 (LONG_PTR)test_proc, proc);
2027 ddraw = create_ddraw();
2028 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2029 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2030 (LONG_PTR)test_proc, proc);
2031 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2032 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2033 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2034 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2035 (LONG_PTR)test_proc, proc);
2036 ref = IDirectDraw7_Release(ddraw);
2037 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2038 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2039 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2040 (LONG_PTR)DefWindowProcA, proc);
2042 fix_wndproc(window, (LONG_PTR)test_proc);
2043 expect_messages = NULL;
2044 DestroyWindow(window);
2045 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2048 static void test_window_style(void)
2050 LONG style, exstyle, tmp;
2051 RECT fullscreen_rect, r;
2052 IDirectDraw7 *ddraw;
2053 HWND window;
2054 HRESULT hr;
2055 ULONG ref;
2057 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2058 0, 0, 100, 100, 0, 0, 0, 0);
2059 ddraw = create_ddraw();
2060 ok(!!ddraw, "Failed to create a ddraw object.\n");
2062 style = GetWindowLongA(window, GWL_STYLE);
2063 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2064 SetRect(&fullscreen_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2066 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2067 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2069 tmp = GetWindowLongA(window, GWL_STYLE);
2070 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2071 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2072 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2074 GetWindowRect(window, &r);
2075 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2076 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2077 r.left, r.top, r.right, r.bottom);
2078 GetClientRect(window, &r);
2079 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2081 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2082 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2084 tmp = GetWindowLongA(window, GWL_STYLE);
2085 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2086 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2087 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2089 ref = IDirectDraw7_Release(ddraw);
2090 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2092 DestroyWindow(window);
2095 static void test_redundant_mode_set(void)
2097 DDSURFACEDESC2 surface_desc = {0};
2098 IDirectDraw7 *ddraw;
2099 HWND window;
2100 HRESULT hr;
2101 RECT r, s;
2102 ULONG ref;
2104 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2105 0, 0, 100, 100, 0, 0, 0, 0);
2106 ddraw = create_ddraw();
2107 ok(!!ddraw, "Failed to create a ddraw object.\n");
2108 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2109 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2111 surface_desc.dwSize = sizeof(surface_desc);
2112 hr = IDirectDraw7_GetDisplayMode(ddraw, &surface_desc);
2113 ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
2115 hr = IDirectDraw7_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2116 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2117 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2119 GetWindowRect(window, &r);
2120 r.right /= 2;
2121 r.bottom /= 2;
2122 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2123 GetWindowRect(window, &s);
2124 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2125 r.left, r.top, r.right, r.bottom,
2126 s.left, s.top, s.right, s.bottom);
2128 hr = IDirectDraw7_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2129 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2130 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2132 GetWindowRect(window, &s);
2133 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2134 r.left, r.top, r.right, r.bottom,
2135 s.left, s.top, s.right, s.bottom);
2137 ref = IDirectDraw7_Release(ddraw);
2138 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2140 DestroyWindow(window);
2143 static SIZE screen_size, screen_size2;
2145 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2147 if (message == WM_SIZE)
2149 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2150 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2153 return test_proc(hwnd, message, wparam, lparam);
2156 static LRESULT CALLBACK mode_set_proc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2158 if (message == WM_SIZE)
2160 screen_size2.cx = GetSystemMetrics(SM_CXSCREEN);
2161 screen_size2.cy = GetSystemMetrics(SM_CYSCREEN);
2164 return test_proc(hwnd, message, wparam, lparam);
2167 static void test_coop_level_mode_set(void)
2169 IDirectDrawSurface7 *primary;
2170 RECT fullscreen_rect, r, s;
2171 IDirectDraw7 *ddraw;
2172 DDSURFACEDESC2 ddsd;
2173 WNDCLASSA wc = {0};
2174 HWND window, window2;
2175 HRESULT hr;
2176 ULONG ref;
2177 MSG msg;
2179 static const UINT exclusive_messages[] =
2181 WM_WINDOWPOSCHANGING,
2182 WM_WINDOWPOSCHANGED,
2183 WM_SIZE,
2184 WM_DISPLAYCHANGE,
2188 static const UINT normal_messages[] =
2190 WM_DISPLAYCHANGE,
2194 ddraw = create_ddraw();
2195 ok(!!ddraw, "Failed to create a ddraw object.\n");
2197 wc.lpfnWndProc = mode_set_proc;
2198 wc.lpszClassName = "ddraw_test_wndproc_wc";
2199 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2200 wc.lpfnWndProc = mode_set_proc2;
2201 wc.lpszClassName = "ddraw_test_wndproc_wc2";
2202 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2204 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2205 0, 0, 100, 100, 0, 0, 0, 0);
2206 window2 = CreateWindowA("ddraw_test_wndproc_wc2", "ddraw_test", WS_OVERLAPPEDWINDOW,
2207 0, 0, 100, 100, 0, 0, 0, 0);
2209 SetRect(&fullscreen_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2210 SetRect(&s, 0, 0, 640, 480);
2212 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2213 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2215 GetWindowRect(window, &r);
2216 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2217 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2218 r.left, r.top, r.right, r.bottom);
2220 memset(&ddsd, 0, sizeof(ddsd));
2221 ddsd.dwSize = sizeof(ddsd);
2222 ddsd.dwFlags = DDSD_CAPS;
2223 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2225 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2226 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2227 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2228 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2229 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2230 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2231 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2232 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2234 GetWindowRect(window, &r);
2235 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2236 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2237 r.left, r.top, r.right, r.bottom);
2239 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2240 expect_messages = exclusive_messages;
2241 screen_size.cx = 0;
2242 screen_size.cy = 0;
2244 hr = set_display_mode(ddraw, 640, 480);
2245 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2247 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2248 expect_messages = NULL;
2249 ok(screen_size.cx == s.right && screen_size.cy == s.bottom,
2250 "Expected screen size %ux%u, got %ux%u.\n",
2251 s.right, s.bottom, screen_size.cx, screen_size.cy);
2253 GetWindowRect(window, &r);
2254 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2255 s.left, s.top, s.right, s.bottom,
2256 r.left, r.top, r.right, r.bottom);
2258 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2259 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2260 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2261 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2262 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2263 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2264 IDirectDrawSurface7_Release(primary);
2266 memset(&ddsd, 0, sizeof(ddsd));
2267 ddsd.dwSize = sizeof(ddsd);
2268 ddsd.dwFlags = DDSD_CAPS;
2269 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2271 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2272 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2273 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2274 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2275 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2276 s.right - s.left, ddsd.dwWidth);
2277 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2278 s.bottom - s.top, ddsd.dwHeight);
2280 GetWindowRect(window, &r);
2281 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2282 s.left, s.top, s.right, s.bottom,
2283 r.left, r.top, r.right, r.bottom);
2285 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2286 expect_messages = exclusive_messages;
2287 screen_size.cx = 0;
2288 screen_size.cy = 0;
2290 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2291 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2293 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2294 expect_messages = NULL;
2295 ok(screen_size.cx == fullscreen_rect.right && screen_size.cy == fullscreen_rect.bottom,
2296 "Expected screen size %ux%u, got %ux%u.\n",
2297 fullscreen_rect.right, fullscreen_rect.bottom, screen_size.cx, screen_size.cy);
2299 GetWindowRect(window, &r);
2300 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2301 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2302 r.left, r.top, r.right, r.bottom);
2304 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2305 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2306 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2307 s.right - s.left, ddsd.dwWidth);
2308 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2309 s.bottom - s.top, ddsd.dwHeight);
2310 IDirectDrawSurface7_Release(primary);
2312 memset(&ddsd, 0, sizeof(ddsd));
2313 ddsd.dwSize = sizeof(ddsd);
2314 ddsd.dwFlags = DDSD_CAPS;
2315 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2317 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2318 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2319 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2320 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2321 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2322 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2323 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2324 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2326 GetWindowRect(window, &r);
2327 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2328 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2329 r.left, r.top, r.right, r.bottom);
2331 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2332 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2334 GetWindowRect(window, &r);
2335 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2336 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2337 r.left, r.top, r.right, r.bottom);
2339 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2340 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2341 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2342 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2343 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2344 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2345 IDirectDrawSurface7_Release(primary);
2347 memset(&ddsd, 0, sizeof(ddsd));
2348 ddsd.dwSize = sizeof(ddsd);
2349 ddsd.dwFlags = DDSD_CAPS;
2350 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2352 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2353 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2354 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2355 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2356 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2357 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2358 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2359 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2361 GetWindowRect(window, &r);
2362 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2363 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2364 r.left, r.top, r.right, r.bottom);
2366 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2367 expect_messages = normal_messages;
2368 screen_size.cx = 0;
2369 screen_size.cy = 0;
2371 hr = set_display_mode(ddraw, 640, 480);
2372 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2374 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2375 expect_messages = NULL;
2376 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2378 GetWindowRect(window, &r);
2379 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2380 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2381 r.left, r.top, r.right, r.bottom);
2383 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2384 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2385 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2386 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2387 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2388 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2389 IDirectDrawSurface7_Release(primary);
2391 memset(&ddsd, 0, sizeof(ddsd));
2392 ddsd.dwSize = sizeof(ddsd);
2393 ddsd.dwFlags = DDSD_CAPS;
2394 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2396 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2397 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2398 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2399 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2400 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2401 s.right - s.left, ddsd.dwWidth);
2402 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2403 s.bottom - s.top, ddsd.dwHeight);
2405 GetWindowRect(window, &r);
2406 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2407 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2408 r.left, r.top, r.right, r.bottom);
2410 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2411 expect_messages = normal_messages;
2412 screen_size.cx = 0;
2413 screen_size.cy = 0;
2415 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2416 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2418 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2419 expect_messages = NULL;
2420 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2422 GetWindowRect(window, &r);
2423 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2424 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2425 r.left, r.top, r.right, r.bottom);
2427 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2428 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2429 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2430 s.right - s.left, ddsd.dwWidth);
2431 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2432 s.bottom - s.top, ddsd.dwHeight);
2433 IDirectDrawSurface7_Release(primary);
2435 memset(&ddsd, 0, sizeof(ddsd));
2436 ddsd.dwSize = sizeof(ddsd);
2437 ddsd.dwFlags = DDSD_CAPS;
2438 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2440 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2441 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2442 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2443 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2444 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2445 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2446 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2447 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2449 GetWindowRect(window, &r);
2450 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2451 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2452 r.left, r.top, r.right, r.bottom);
2454 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
2455 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
2456 * not DDSCL_FULLSCREEN. */
2457 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2458 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2460 GetWindowRect(window, &r);
2461 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2462 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2463 r.left, r.top, r.right, r.bottom);
2465 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2466 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2467 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2468 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2469 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2470 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2471 IDirectDrawSurface7_Release(primary);
2473 memset(&ddsd, 0, sizeof(ddsd));
2474 ddsd.dwSize = sizeof(ddsd);
2475 ddsd.dwFlags = DDSD_CAPS;
2476 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2478 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2479 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2480 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2481 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2482 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2483 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2484 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2485 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2487 GetWindowRect(window, &r);
2488 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2489 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2490 r.left, r.top, r.right, r.bottom);
2492 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2493 expect_messages = normal_messages;
2494 screen_size.cx = 0;
2495 screen_size.cy = 0;
2497 hr = set_display_mode(ddraw, 640, 480);
2498 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2500 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2501 expect_messages = NULL;
2502 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2504 GetWindowRect(window, &r);
2505 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2506 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2507 r.left, r.top, r.right, r.bottom);
2509 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2510 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2511 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2512 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2513 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2514 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2515 IDirectDrawSurface7_Release(primary);
2517 memset(&ddsd, 0, sizeof(ddsd));
2518 ddsd.dwSize = sizeof(ddsd);
2519 ddsd.dwFlags = DDSD_CAPS;
2520 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2522 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2523 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2524 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2525 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2526 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2527 s.right - s.left, ddsd.dwWidth);
2528 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2529 s.bottom - s.top, ddsd.dwHeight);
2531 GetWindowRect(window, &r);
2532 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2533 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2534 r.left, r.top, r.right, r.bottom);
2536 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2537 expect_messages = normal_messages;
2538 screen_size.cx = 0;
2539 screen_size.cy = 0;
2541 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2542 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2544 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2545 expect_messages = NULL;
2546 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2548 GetWindowRect(window, &r);
2549 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2550 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2551 r.left, r.top, r.right, r.bottom);
2553 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2554 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2555 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2556 s.right - s.left, ddsd.dwWidth);
2557 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2558 s.bottom - s.top, ddsd.dwHeight);
2559 IDirectDrawSurface7_Release(primary);
2561 memset(&ddsd, 0, sizeof(ddsd));
2562 ddsd.dwSize = sizeof(ddsd);
2563 ddsd.dwFlags = DDSD_CAPS;
2564 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2566 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2567 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2568 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2569 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2570 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2571 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2572 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2573 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2574 IDirectDrawSurface7_Release(primary);
2576 GetWindowRect(window, &r);
2577 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2578 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2579 r.left, r.top, r.right, r.bottom);
2581 /* Changing the coop level from EXCLUSIVE to NORMAL restores the screen resolution */
2582 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2583 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2584 hr = set_display_mode(ddraw, 640, 480);
2585 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2587 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2588 expect_messages = exclusive_messages;
2589 screen_size.cx = 0;
2590 screen_size.cy = 0;
2592 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2593 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2595 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2596 expect_messages = NULL;
2597 ok(screen_size.cx == fullscreen_rect.right && screen_size.cy == fullscreen_rect.bottom,
2598 "Expected screen size %ux%u, got %ux%u.\n",
2599 fullscreen_rect.right, fullscreen_rect.bottom, screen_size.cx, screen_size.cy);
2601 GetWindowRect(window, &r);
2602 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2603 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2604 r.left, r.top, r.right, r.bottom);
2606 memset(&ddsd, 0, sizeof(ddsd));
2607 ddsd.dwSize = sizeof(ddsd);
2608 ddsd.dwFlags = DDSD_CAPS;
2609 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2611 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2612 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2613 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2614 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2615 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2616 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2617 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2618 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2619 IDirectDrawSurface7_Release(primary);
2621 /* The screen restore is a property of DDSCL_EXCLUSIVE */
2622 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2623 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2624 hr = set_display_mode(ddraw, 640, 480);
2625 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2627 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2628 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2630 memset(&ddsd, 0, sizeof(ddsd));
2631 ddsd.dwSize = sizeof(ddsd);
2632 ddsd.dwFlags = DDSD_CAPS;
2633 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2635 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2636 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2637 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2638 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2639 ok(ddsd.dwWidth == s.right - s.left, "Expected surface width %u, got %u.\n",
2640 s.right - s.left, ddsd.dwWidth);
2641 ok(ddsd.dwHeight == s.bottom - s.top, "Expected surface height %u, got %u.\n",
2642 s.bottom - s.top, ddsd.dwHeight);
2643 IDirectDrawSurface7_Release(primary);
2645 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2646 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2648 /* If the window is changed at the same time, messages are sent to the new window. */
2649 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2650 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2651 hr = set_display_mode(ddraw, 640, 480);
2652 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2654 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2655 expect_messages = exclusive_messages;
2656 screen_size.cx = 0;
2657 screen_size.cy = 0;
2658 screen_size2.cx = 0;
2659 screen_size2.cy = 0;
2661 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
2662 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2664 ok(!*expect_messages, "Expected message %#x, but didn't receive it.\n", *expect_messages);
2665 expect_messages = NULL;
2666 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n",
2667 screen_size.cx, screen_size.cy);
2668 ok(screen_size2.cx == fullscreen_rect.right && screen_size2.cy == fullscreen_rect.bottom,
2669 "Expected screen size 2 %ux%u, got %ux%u.\n",
2670 fullscreen_rect.right, fullscreen_rect.bottom, screen_size2.cx, screen_size2.cy);
2672 GetWindowRect(window, &r);
2673 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2674 s.left, s.top, s.right, s.bottom,
2675 r.left, r.top, r.right, r.bottom);
2676 GetWindowRect(window2, &r);
2677 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2678 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2679 r.left, r.top, r.right, r.bottom);
2681 memset(&ddsd, 0, sizeof(ddsd));
2682 ddsd.dwSize = sizeof(ddsd);
2683 ddsd.dwFlags = DDSD_CAPS;
2684 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2686 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
2687 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2688 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &ddsd);
2689 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2690 ok(ddsd.dwWidth == fullscreen_rect.right - fullscreen_rect.left, "Expected surface width %u, got %u.\n",
2691 fullscreen_rect.right - fullscreen_rect.left, ddsd.dwWidth);
2692 ok(ddsd.dwHeight == fullscreen_rect.bottom - fullscreen_rect.top, "Expected surface height %u, got %u.\n",
2693 fullscreen_rect.bottom - fullscreen_rect.top, ddsd.dwHeight);
2694 IDirectDrawSurface7_Release(primary);
2696 ref = IDirectDraw7_Release(ddraw);
2697 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2699 GetWindowRect(window, &r);
2700 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2701 s.left, s.top, s.right, s.bottom,
2702 r.left, r.top, r.right, r.bottom);
2704 expect_messages = NULL;
2705 DestroyWindow(window);
2706 DestroyWindow(window2);
2707 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2708 UnregisterClassA("ddraw_test_wndproc_wc2", GetModuleHandleA(NULL));
2711 static void test_coop_level_mode_set_multi(void)
2713 IDirectDraw7 *ddraw1, *ddraw2;
2714 UINT orig_w, orig_h, w, h;
2715 HWND window;
2716 HRESULT hr;
2717 ULONG ref;
2719 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2720 0, 0, 100, 100, 0, 0, 0, 0);
2721 ddraw1 = create_ddraw();
2722 ok(!!ddraw1, "Failed to create a ddraw object.\n");
2724 orig_w = GetSystemMetrics(SM_CXSCREEN);
2725 orig_h = GetSystemMetrics(SM_CYSCREEN);
2727 /* With just a single ddraw object, the display mode is restored on
2728 * release. */
2729 hr = set_display_mode(ddraw1, 800, 600);
2730 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2731 w = GetSystemMetrics(SM_CXSCREEN);
2732 ok(w == 800, "Got unexpected screen width %u.\n", w);
2733 h = GetSystemMetrics(SM_CYSCREEN);
2734 ok(h == 600, "Got unexpected screen height %u.\n", h);
2736 ref = IDirectDraw7_Release(ddraw1);
2737 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2738 w = GetSystemMetrics(SM_CXSCREEN);
2739 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2740 h = GetSystemMetrics(SM_CYSCREEN);
2741 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2743 /* When there are multiple ddraw objects, the display mode is restored to
2744 * the initial mode, before the first SetDisplayMode() call. */
2745 ddraw1 = create_ddraw();
2746 hr = set_display_mode(ddraw1, 800, 600);
2747 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2748 w = GetSystemMetrics(SM_CXSCREEN);
2749 ok(w == 800, "Got unexpected screen width %u.\n", w);
2750 h = GetSystemMetrics(SM_CYSCREEN);
2751 ok(h == 600, "Got unexpected screen height %u.\n", h);
2753 ddraw2 = create_ddraw();
2754 hr = set_display_mode(ddraw2, 640, 480);
2755 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2756 w = GetSystemMetrics(SM_CXSCREEN);
2757 ok(w == 640, "Got unexpected screen width %u.\n", w);
2758 h = GetSystemMetrics(SM_CYSCREEN);
2759 ok(h == 480, "Got unexpected screen height %u.\n", h);
2761 ref = IDirectDraw7_Release(ddraw2);
2762 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2763 w = GetSystemMetrics(SM_CXSCREEN);
2764 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2765 h = GetSystemMetrics(SM_CYSCREEN);
2766 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2768 ref = IDirectDraw7_Release(ddraw1);
2769 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2770 w = GetSystemMetrics(SM_CXSCREEN);
2771 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2772 h = GetSystemMetrics(SM_CYSCREEN);
2773 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2775 /* Regardless of release ordering. */
2776 ddraw1 = create_ddraw();
2777 hr = set_display_mode(ddraw1, 800, 600);
2778 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2779 w = GetSystemMetrics(SM_CXSCREEN);
2780 ok(w == 800, "Got unexpected screen width %u.\n", w);
2781 h = GetSystemMetrics(SM_CYSCREEN);
2782 ok(h == 600, "Got unexpected screen height %u.\n", h);
2784 ddraw2 = create_ddraw();
2785 hr = set_display_mode(ddraw2, 640, 480);
2786 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2787 w = GetSystemMetrics(SM_CXSCREEN);
2788 ok(w == 640, "Got unexpected screen width %u.\n", w);
2789 h = GetSystemMetrics(SM_CYSCREEN);
2790 ok(h == 480, "Got unexpected screen height %u.\n", h);
2792 ref = IDirectDraw7_Release(ddraw1);
2793 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2794 w = GetSystemMetrics(SM_CXSCREEN);
2795 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2796 h = GetSystemMetrics(SM_CYSCREEN);
2797 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2799 ref = IDirectDraw7_Release(ddraw2);
2800 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2801 w = GetSystemMetrics(SM_CXSCREEN);
2802 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2803 h = GetSystemMetrics(SM_CYSCREEN);
2804 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2806 /* But only for ddraw objects that called SetDisplayMode(). */
2807 ddraw1 = create_ddraw();
2808 ddraw2 = create_ddraw();
2809 hr = set_display_mode(ddraw2, 640, 480);
2810 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2811 w = GetSystemMetrics(SM_CXSCREEN);
2812 ok(w == 640, "Got unexpected screen width %u.\n", w);
2813 h = GetSystemMetrics(SM_CYSCREEN);
2814 ok(h == 480, "Got unexpected screen height %u.\n", h);
2816 ref = IDirectDraw7_Release(ddraw1);
2817 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2818 w = GetSystemMetrics(SM_CXSCREEN);
2819 ok(w == 640, "Got unexpected screen width %u.\n", w);
2820 h = GetSystemMetrics(SM_CYSCREEN);
2821 ok(h == 480, "Got unexpected screen height %u.\n", h);
2823 ref = IDirectDraw7_Release(ddraw2);
2824 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2825 w = GetSystemMetrics(SM_CXSCREEN);
2826 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2827 h = GetSystemMetrics(SM_CYSCREEN);
2828 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2830 /* If there's a ddraw object that's currently in exclusive mode, it blocks
2831 * restoring the display mode. */
2832 ddraw1 = create_ddraw();
2833 hr = set_display_mode(ddraw1, 800, 600);
2834 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2835 w = GetSystemMetrics(SM_CXSCREEN);
2836 ok(w == 800, "Got unexpected screen width %u.\n", w);
2837 h = GetSystemMetrics(SM_CYSCREEN);
2838 ok(h == 600, "Got unexpected screen height %u.\n", h);
2840 ddraw2 = create_ddraw();
2841 hr = set_display_mode(ddraw2, 640, 480);
2842 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2843 w = GetSystemMetrics(SM_CXSCREEN);
2844 ok(w == 640, "Got unexpected screen width %u.\n", w);
2845 h = GetSystemMetrics(SM_CYSCREEN);
2846 ok(h == 480, "Got unexpected screen height %u.\n", h);
2848 hr = IDirectDraw7_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2849 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2851 ref = IDirectDraw7_Release(ddraw1);
2852 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2853 w = GetSystemMetrics(SM_CXSCREEN);
2854 ok(w == 640, "Got unexpected screen width %u.\n", w);
2855 h = GetSystemMetrics(SM_CYSCREEN);
2856 ok(h == 480, "Got unexpected screen height %u.\n", h);
2858 ref = IDirectDraw7_Release(ddraw2);
2859 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2860 w = GetSystemMetrics(SM_CXSCREEN);
2861 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2862 h = GetSystemMetrics(SM_CYSCREEN);
2863 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2865 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
2866 ddraw1 = create_ddraw();
2867 hr = set_display_mode(ddraw1, 800, 600);
2868 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2869 w = GetSystemMetrics(SM_CXSCREEN);
2870 ok(w == 800, "Got unexpected screen width %u.\n", w);
2871 h = GetSystemMetrics(SM_CYSCREEN);
2872 ok(h == 600, "Got unexpected screen height %u.\n", h);
2874 hr = IDirectDraw7_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2875 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2877 ddraw2 = create_ddraw();
2878 hr = set_display_mode(ddraw2, 640, 480);
2879 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
2881 ref = IDirectDraw7_Release(ddraw1);
2882 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2883 w = GetSystemMetrics(SM_CXSCREEN);
2884 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2885 h = GetSystemMetrics(SM_CYSCREEN);
2886 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2888 ref = IDirectDraw7_Release(ddraw2);
2889 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2890 w = GetSystemMetrics(SM_CXSCREEN);
2891 ok(w == orig_w, "Got unexpected screen width %u.\n", w);
2892 h = GetSystemMetrics(SM_CYSCREEN);
2893 ok(h == orig_h, "Got unexpected screen height %u.\n", h);
2895 DestroyWindow(window);
2898 static void test_initialize(void)
2900 IDirectDraw7 *ddraw;
2901 HRESULT hr;
2903 ddraw = create_ddraw();
2904 ok(!!ddraw, "Failed to create a ddraw object.\n");
2906 hr = IDirectDraw7_Initialize(ddraw, NULL);
2907 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
2908 IDirectDraw7_Release(ddraw);
2910 CoInitialize(NULL);
2911 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw7, (void **)&ddraw);
2912 ok(SUCCEEDED(hr), "Failed to create IDirectDraw7 instance, hr %#x.\n", hr);
2913 hr = IDirectDraw7_Initialize(ddraw, NULL);
2914 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
2915 hr = IDirectDraw7_Initialize(ddraw, NULL);
2916 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
2917 IDirectDraw7_Release(ddraw);
2918 CoUninitialize();
2921 static void test_coop_level_surf_create(void)
2923 IDirectDrawSurface7 *surface;
2924 IDirectDraw7 *ddraw;
2925 DDSURFACEDESC2 ddsd;
2926 HRESULT hr;
2928 ddraw = create_ddraw();
2929 ok(!!ddraw, "Failed to create a ddraw object.\n");
2931 memset(&ddsd, 0, sizeof(ddsd));
2932 ddsd.dwSize = sizeof(ddsd);
2933 ddsd.dwFlags = DDSD_CAPS;
2934 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2935 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
2936 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
2938 IDirectDraw7_Release(ddraw);
2941 static void test_vb_discard(void)
2943 static const struct vec4 quad[] =
2945 { 0.0f, 480.0f, 0.0f, 1.0f},
2946 { 0.0f, 0.0f, 0.0f, 1.0f},
2947 {640.0f, 480.0f, 0.0f, 1.0f},
2948 {640.0f, 0.0f, 0.0f, 1.0f},
2951 IDirect3DDevice7 *device;
2952 IDirect3D7 *d3d;
2953 IDirect3DVertexBuffer7 *buffer;
2954 HWND window;
2955 HRESULT hr;
2956 D3DVERTEXBUFFERDESC desc;
2957 BYTE *data;
2958 static const unsigned int vbsize = 16;
2959 unsigned int i;
2961 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2962 0, 0, 640, 480, 0, 0, 0, 0);
2964 if (!(device = create_device(window, DDSCL_NORMAL)))
2966 skip("Failed to create a 3D device, skipping test.\n");
2967 DestroyWindow(window);
2968 return;
2971 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
2972 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
2974 memset(&desc, 0, sizeof(desc));
2975 desc.dwSize = sizeof(desc);
2976 desc.dwCaps = D3DVBCAPS_WRITEONLY;
2977 desc.dwFVF = D3DFVF_XYZRHW;
2978 desc.dwNumVertices = vbsize;
2979 hr = IDirect3D7_CreateVertexBuffer(d3d, &desc, &buffer, 0);
2980 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
2982 hr = IDirect3DVertexBuffer7_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
2983 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
2984 memcpy(data, quad, sizeof(quad));
2985 hr = IDirect3DVertexBuffer7_Unlock(buffer);
2986 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
2988 hr = IDirect3DDevice7_BeginScene(device);
2989 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
2990 hr = IDirect3DDevice7_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
2991 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
2992 hr = IDirect3DDevice7_EndScene(device);
2993 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
2995 hr = IDirect3DVertexBuffer7_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
2996 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
2997 memset(data, 0xaa, sizeof(struct vec4) * vbsize);
2998 hr = IDirect3DVertexBuffer7_Unlock(buffer);
2999 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3001 hr = IDirect3DVertexBuffer7_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3002 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3003 for (i = 0; i < sizeof(struct vec4) * vbsize; i++)
3005 if (data[i] != 0xaa)
3007 ok(FALSE, "Vertex buffer data byte %u is 0x%02x, expected 0xaa\n", i, data[i]);
3008 break;
3011 hr = IDirect3DVertexBuffer7_Unlock(buffer);
3012 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3014 IDirect3DVertexBuffer7_Release(buffer);
3015 IDirect3D7_Release(d3d);
3016 IDirect3DDevice7_Release(device);
3017 DestroyWindow(window);
3020 static void test_coop_level_multi_window(void)
3022 HWND window1, window2;
3023 IDirectDraw7 *ddraw;
3024 HRESULT hr;
3026 window1 = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3027 0, 0, 640, 480, 0, 0, 0, 0);
3028 window2 = CreateWindowA("static", "ddraw_test2", WS_OVERLAPPEDWINDOW,
3029 0, 0, 640, 480, 0, 0, 0, 0);
3030 ddraw = create_ddraw();
3031 ok(!!ddraw, "Failed to create a ddraw object.\n");
3033 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3034 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3035 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3036 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3037 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3038 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3040 IDirectDraw7_Release(ddraw);
3041 DestroyWindow(window2);
3042 DestroyWindow(window1);
3045 static void test_draw_strided(void)
3047 static struct vec3 position[] =
3049 {-1.0, -1.0, 0.0},
3050 {-1.0, 1.0, 0.0},
3051 { 1.0, 1.0, 0.0},
3052 { 1.0, -1.0, 0.0},
3054 static DWORD diffuse[] =
3056 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00,
3058 static WORD indices[] =
3060 0, 1, 2, 2, 3, 0
3063 IDirectDrawSurface7 *rt;
3064 IDirect3DDevice7 *device;
3065 D3DCOLOR color;
3066 HWND window;
3067 HRESULT hr;
3068 D3DDRAWPRIMITIVESTRIDEDDATA strided;
3070 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3071 0, 0, 640, 480, 0, 0, 0, 0);
3073 if (!(device = create_device(window, DDSCL_NORMAL)))
3075 skip("Failed to create a 3D device, skipping test.\n");
3076 DestroyWindow(window);
3077 return;
3080 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
3081 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3083 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3084 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3085 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x00000000, 1.0f, 0);
3086 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
3087 hr = IDirect3DDevice7_BeginScene(device);
3088 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3090 memset(&strided, 0x55, sizeof(strided));
3091 strided.position.lpvData = position;
3092 strided.position.dwStride = sizeof(*position);
3093 strided.diffuse.lpvData = diffuse;
3094 strided.diffuse.dwStride = sizeof(*diffuse);
3095 hr = IDirect3DDevice7_DrawIndexedPrimitiveStrided(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE,
3096 &strided, 4, indices, 6, 0);
3097 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3099 hr = IDirect3DDevice7_EndScene(device);
3100 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3102 color = get_surface_color(rt, 320, 240);
3103 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
3105 IDirectDrawSurface7_Release(rt);
3106 IDirect3DDevice7_Release(device);
3107 DestroyWindow(window);
3110 static void test_clear_rect_count(void)
3112 IDirectDrawSurface7 *rt;
3113 IDirect3DDevice7 *device;
3114 D3DCOLOR color;
3115 HWND window;
3116 HRESULT hr;
3117 D3DRECT rect = {{0}, {0}, {640}, {480}};
3119 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3120 0, 0, 640, 480, 0, 0, 0, 0);
3121 if (!(device = create_device(window, DDSCL_NORMAL)))
3123 skip("Failed to create a 3D device, skipping test.\n");
3124 DestroyWindow(window);
3125 return;
3128 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
3129 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3131 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x00ffffff, 1.0f, 0);
3132 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
3133 hr = IDirect3DDevice7_Clear(device, 0, &rect, D3DCLEAR_TARGET, 0x00ff0000, 1.0f, 0);
3134 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
3136 color = get_surface_color(rt, 320, 240);
3137 ok(compare_color(color, 0x00ffffff, 1),
3138 "Clear with count = 0, rect != NULL has color %#08x.\n", color);
3140 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x00ffffff, 1.0f, 0);
3141 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
3142 hr = IDirect3DDevice7_Clear(device, 1, NULL, D3DCLEAR_TARGET, 0x0000ff00, 1.0f, 0);
3143 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
3145 color = get_surface_color(rt, 320, 240);
3146 ok(compare_color(color, 0x0000ff00, 1),
3147 "Clear with count = 1, rect = NULL has color %#08x.\n", color);
3149 IDirectDrawSurface7_Release(rt);
3150 IDirect3DDevice7_Release(device);
3151 DestroyWindow(window);
3154 static BOOL test_mode_restored(IDirectDraw7 *ddraw, HWND window)
3156 DDSURFACEDESC2 ddsd1, ddsd2;
3157 HRESULT hr;
3159 memset(&ddsd1, 0, sizeof(ddsd1));
3160 ddsd1.dwSize = sizeof(ddsd1);
3161 hr = IDirectDraw7_GetDisplayMode(ddraw, &ddsd1);
3162 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
3164 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3165 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3166 hr = set_display_mode(ddraw, 640, 480);
3167 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3168 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3169 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3171 memset(&ddsd2, 0, sizeof(ddsd2));
3172 ddsd2.dwSize = sizeof(ddsd2);
3173 hr = IDirectDraw7_GetDisplayMode(ddraw, &ddsd2);
3174 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
3175 hr = IDirectDraw7_RestoreDisplayMode(ddraw);
3176 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3178 return ddsd1.dwWidth == ddsd2.dwWidth && ddsd1.dwHeight == ddsd2.dwHeight;
3181 static void test_coop_level_versions(void)
3183 HWND window;
3184 IDirectDraw *ddraw;
3185 HRESULT hr;
3186 BOOL restored;
3187 IDirectDrawSurface *surface;
3188 IDirectDraw7 *ddraw7;
3189 DDSURFACEDESC ddsd;
3191 window = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3192 0, 0, 640, 480, 0, 0, 0, 0);
3194 ddraw7 = create_ddraw();
3195 ok(!!ddraw7, "Failed to create a ddraw object.\n");
3196 /* Newly created ddraw objects restore the mode on ddraw2+::SetCooperativeLevel(NORMAL) */
3197 restored = test_mode_restored(ddraw7, window);
3198 ok(restored, "Display mode not restored in new ddraw object\n");
3200 /* A failing ddraw1::SetCooperativeLevel call does not have an effect */
3201 hr = IDirectDraw7_QueryInterface(ddraw7, &IID_IDirectDraw, (void **)&ddraw);
3202 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3204 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3205 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
3206 restored = test_mode_restored(ddraw7, window);
3207 ok(restored, "Display mode not restored after bad ddraw1::SetCooperativeLevel call\n");
3209 /* A successful one does */
3210 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3211 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3212 restored = test_mode_restored(ddraw7, window);
3213 ok(!restored, "Display mode restored after good ddraw1::SetCooperativeLevel call\n");
3215 IDirectDraw_Release(ddraw);
3216 IDirectDraw7_Release(ddraw7);
3218 ddraw7 = create_ddraw();
3219 ok(!!ddraw7, "Failed to create a ddraw object.\n");
3220 hr = IDirectDraw7_QueryInterface(ddraw7, &IID_IDirectDraw, (void **)&ddraw);
3221 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3223 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_SETFOCUSWINDOW);
3224 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3225 restored = test_mode_restored(ddraw7, window);
3226 ok(!restored, "Display mode restored after ddraw1::SetCooperativeLevel(SETFOCUSWINDOW) call\n");
3228 IDirectDraw_Release(ddraw);
3229 IDirectDraw7_Release(ddraw7);
3231 /* A failing call does not restore the ddraw2+ behavior */
3232 ddraw7 = create_ddraw();
3233 ok(!!ddraw7, "Failed to create a ddraw object.\n");
3234 hr = IDirectDraw7_QueryInterface(ddraw7, &IID_IDirectDraw, (void **)&ddraw);
3235 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3237 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3238 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3239 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3240 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
3241 restored = test_mode_restored(ddraw7, window);
3242 ok(!restored, "Display mode restored after good-bad ddraw1::SetCooperativeLevel() call sequence\n");
3244 IDirectDraw_Release(ddraw);
3245 IDirectDraw7_Release(ddraw7);
3247 /* Neither does a sequence of successful calls with the new interface */
3248 ddraw7 = create_ddraw();
3249 ok(!!ddraw7, "Failed to create a ddraw object.\n");
3250 hr = IDirectDraw7_QueryInterface(ddraw7, &IID_IDirectDraw, (void **)&ddraw);
3251 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3253 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3254 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3255 hr = IDirectDraw7_SetCooperativeLevel(ddraw7, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3256 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3257 hr = IDirectDraw7_SetCooperativeLevel(ddraw7, window, DDSCL_NORMAL);
3258 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3260 restored = test_mode_restored(ddraw7, window);
3261 ok(!restored, "Display mode restored after ddraw1-ddraw7 SetCooperativeLevel() call sequence\n");
3262 IDirectDraw_Release(ddraw);
3263 IDirectDraw7_Release(ddraw7);
3265 /* ddraw1::CreateSurface does not triger the ddraw1 behavior */
3266 ddraw7 = create_ddraw();
3267 ok(!!ddraw7, "Failed to create a ddraw object.\n");
3268 hr = IDirectDraw7_QueryInterface(ddraw7, &IID_IDirectDraw, (void **)&ddraw);
3269 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3271 hr = IDirectDraw7_SetCooperativeLevel(ddraw7, window, DDSCL_NORMAL);
3272 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3274 memset(&ddsd, 0, sizeof(ddsd));
3275 ddsd.dwSize = sizeof(ddsd);
3276 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3277 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3278 ddsd.dwWidth = ddsd.dwHeight = 8;
3279 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3280 ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
3281 IDirectDrawSurface_Release(surface);
3282 restored = test_mode_restored(ddraw7, window);
3283 ok(restored, "Display mode not restored after ddraw1::CreateSurface() call\n");
3285 IDirectDraw_Release(ddraw);
3286 IDirectDraw7_Release(ddraw7);
3287 DestroyWindow(window);
3290 static void test_fog_special(void)
3292 static struct
3294 struct vec3 position;
3295 D3DCOLOR diffuse;
3297 quad[] =
3299 {{ -1.0f, 1.0f, 0.0f}, 0xff00ff00},
3300 {{ 1.0f, 1.0f, 1.0f}, 0xff00ff00},
3301 {{ -1.0f, -1.0f, 0.0f}, 0xff00ff00},
3302 {{ 1.0f, -1.0f, 1.0f}, 0xff00ff00},
3304 static const struct
3306 DWORD vertexmode, tablemode;
3307 D3DCOLOR color_left, color_right;
3309 tests[] =
3311 {D3DFOG_LINEAR, D3DFOG_NONE, 0x00ff0000, 0x00ff0000},
3312 {D3DFOG_NONE, D3DFOG_LINEAR, 0x0000ff00, 0x00ff0000},
3314 union
3316 float f;
3317 DWORD d;
3318 } conv;
3319 D3DCOLOR color;
3320 HRESULT hr;
3321 unsigned int i;
3322 HWND window;
3323 IDirect3DDevice7 *device;
3324 IDirectDrawSurface7 *rt;
3326 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3327 0, 0, 640, 480, 0, 0, 0, 0);
3329 if (!(device = create_device(window, DDSCL_NORMAL)))
3331 skip("Failed to create a 3D device, skipping test.\n");
3332 DestroyWindow(window);
3333 return;
3336 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
3337 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3339 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, TRUE);
3340 ok(SUCCEEDED(hr), "Failed to enable fog, hr %#x.\n", hr);
3341 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGCOLOR, 0xffff0000);
3342 ok(SUCCEEDED(hr), "Failed to set fog color, hr %#x.\n", hr);
3343 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, FALSE);
3344 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3345 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
3346 ok(SUCCEEDED(hr), "Failed to disable lighting, hr %#x.\n", hr);
3348 conv.f = 0.5f;
3349 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGSTART, conv.d);
3350 ok(SUCCEEDED(hr), "Failed to set fog start, hr %#x.\n", hr);
3351 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGEND, conv.d);
3352 ok(SUCCEEDED(hr), "Failed to set fog end, hr %#x.\n", hr);
3354 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
3356 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x000000ff, 1.0f, 0);
3357 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
3359 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGVERTEXMODE, tests[i].vertexmode);
3360 ok(SUCCEEDED(hr), "Failed to set fogvertexmode, hr %#x.\n", hr);
3361 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGTABLEMODE, tests[i].tablemode);
3362 ok(SUCCEEDED(hr), "Failed to set fogtablemode, hr %#x.\n", hr);
3364 hr = IDirect3DDevice7_BeginScene(device);
3365 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3366 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE, quad, 4, 0);
3367 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3368 hr = IDirect3DDevice7_EndScene(device);
3369 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3371 color = get_surface_color(rt, 310, 240);
3372 ok(compare_color(color, tests[i].color_left, 1),
3373 "Expected left color 0x%08x, got 0x%08x, case %u.\n", tests[i].color_left, color, i);
3374 color = get_surface_color(rt, 330, 240);
3375 ok(compare_color(color, tests[i].color_right, 1),
3376 "Expected right color 0x%08x, got 0x%08x, case %u.\n", tests[i].color_right, color, i);
3379 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_FOGENABLE, FALSE);
3380 ok(SUCCEEDED(hr), "Failed to disable fog, hr %#x.\n", hr);
3382 IDirectDrawSurface7_Release(rt);
3383 IDirect3DDevice7_Release(device);
3384 DestroyWindow(window);
3387 static void test_lighting_interface_versions(void)
3389 IDirect3DDevice7 *device;
3390 IDirectDrawSurface7 *rt;
3391 D3DCOLOR color;
3392 HWND window;
3393 HRESULT hr;
3394 DWORD rs;
3395 unsigned int i;
3396 ULONG ref;
3397 D3DMATERIAL7 material;
3398 static D3DVERTEX quad[] =
3400 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3401 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3402 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3403 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3406 #define FVF_COLORVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR)
3407 static struct
3409 struct vec3 position;
3410 struct vec3 normal;
3411 DWORD diffuse, specular;
3413 quad2[] =
3415 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3416 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3417 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3418 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3421 static D3DLVERTEX lquad[] =
3423 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3424 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3425 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3426 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3429 #define FVF_LVERTEX2 (D3DFVF_LVERTEX & ~D3DFVF_RESERVED1)
3430 static struct
3432 struct vec3 position;
3433 DWORD diffuse, specular;
3434 struct vec2 texcoord;
3436 lquad2[] =
3438 {{-1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
3439 {{ 1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
3440 {{-1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
3441 {{ 1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
3444 static D3DTLVERTEX tlquad[] =
3446 {{ 0.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3447 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3448 {{ 640.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3449 {{ 640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3452 static const struct
3454 DWORD vertextype;
3455 void *data;
3456 DWORD d3drs_lighting, d3drs_specular;
3457 DWORD draw_flags;
3458 D3DCOLOR color;
3460 tests[] =
3462 /* Lighting is enabled when D3DFVF_XYZ is used and D3DRENDERSTATE_LIGHTING is
3463 * enabled. D3DDP_DONOTLIGHT is ignored. Lighting is also enabled when normals
3464 * are not available
3466 * Note that the specular result is 0x00000000 when lighting is on even if the
3467 * input vertex has specular color because D3DRENDERSTATE_COLORVERTEX is not
3468 * enabled */
3470 /* 0 */
3471 { D3DFVF_VERTEX, quad, FALSE, FALSE, 0, 0x00ffffff},
3472 { D3DFVF_VERTEX, quad, TRUE, FALSE, 0, 0x0000ff00},
3473 { D3DFVF_VERTEX, quad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
3474 { D3DFVF_VERTEX, quad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x0000ff00},
3475 { D3DFVF_VERTEX, quad, FALSE, TRUE, 0, 0x00ffffff},
3476 { D3DFVF_VERTEX, quad, TRUE, TRUE, 0, 0x0000ff00},
3477 { D3DFVF_VERTEX, quad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
3478 { D3DFVF_VERTEX, quad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x0000ff00},
3480 /* 8 */
3481 { FVF_COLORVERTEX, quad2, FALSE, FALSE, 0, 0x00ff0000},
3482 { FVF_COLORVERTEX, quad2, TRUE, FALSE, 0, 0x0000ff00},
3483 { FVF_COLORVERTEX, quad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
3484 { FVF_COLORVERTEX, quad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x0000ff00},
3485 { FVF_COLORVERTEX, quad2, FALSE, TRUE, 0, 0x00ff8080},
3486 { FVF_COLORVERTEX, quad2, TRUE, TRUE, 0, 0x0000ff00},
3487 { FVF_COLORVERTEX, quad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
3488 { FVF_COLORVERTEX, quad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x0000ff00},
3490 /* 16 */
3491 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, 0, 0x00ff0000},
3492 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, 0, 0x0000ff00},
3493 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
3494 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x0000ff00},
3495 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, 0, 0x00ff8080},
3496 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, 0, 0x0000ff00},
3497 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
3498 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x0000ff00},
3500 /* 24 */
3501 { FVF_LVERTEX2, lquad2, FALSE, FALSE, 0, 0x00ff0000},
3502 { FVF_LVERTEX2, lquad2, TRUE, FALSE, 0, 0x0000ff00},
3503 { FVF_LVERTEX2, lquad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
3504 { FVF_LVERTEX2, lquad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x0000ff00},
3505 { FVF_LVERTEX2, lquad2, FALSE, TRUE, 0, 0x00ff8080},
3506 { FVF_LVERTEX2, lquad2, TRUE, TRUE, 0, 0x0000ff00},
3507 { FVF_LVERTEX2, lquad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
3508 { FVF_LVERTEX2, lquad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x0000ff00},
3510 /* 32 */
3511 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, 0, 0x000000ff},
3512 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, 0, 0x000000ff},
3513 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
3514 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
3515 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, 0, 0x008080ff},
3516 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, 0, 0x008080ff},
3517 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
3518 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
3521 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3522 0, 0, 640, 480, 0, 0, 0, 0);
3524 if (!(device = create_device(window, DDSCL_NORMAL)))
3526 skip("Failed to create a 3D device, skipping test.\n");
3527 DestroyWindow(window);
3528 return;
3531 hr = IDirect3DDevice7_GetRenderTarget(device, &rt);
3532 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3534 memset(&material, 0, sizeof(material));
3535 U2(U3(material).emissive).g = 1.0f;
3536 hr = IDirect3DDevice7_SetMaterial(device, &material);
3537 ok(SUCCEEDED(hr), "Failed set material, hr %#x.\n", hr);
3538 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
3539 ok(SUCCEEDED(hr), "Failed to disable z test, hr %#x.\n", hr);
3541 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_LIGHTING, &rs);
3542 ok(SUCCEEDED(hr), "Failed to get lighting render state, hr %#x.\n", hr);
3543 ok(rs == TRUE, "Initial D3DRENDERSTATE_LIGHTING is %#x, expected TRUE.\n", rs);
3544 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, &rs);
3545 ok(SUCCEEDED(hr), "Failed to get specularenable render state, hr %#x.\n", hr);
3546 ok(rs == FALSE, "Initial D3DRENDERSTATE_SPECULARENABLE is %#x, expected FALSE.\n", rs);
3548 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
3550 hr = IDirect3DDevice7_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0xff202020, 0.0f, 0);
3551 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3553 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_LIGHTING, tests[i].d3drs_lighting);
3554 ok(SUCCEEDED(hr), "Failed to set lighting render state, hr %#x.\n", hr);
3555 hr = IDirect3DDevice7_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE,
3556 tests[i].d3drs_specular);
3557 ok(SUCCEEDED(hr), "Failed to set specularenable render state, hr %#x.\n", hr);
3559 hr = IDirect3DDevice7_BeginScene(device);
3560 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3561 hr = IDirect3DDevice7_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
3562 tests[i].vertextype, tests[i].data, 4, tests[i].draw_flags | D3DDP_WAIT);
3563 hr = IDirect3DDevice7_EndScene(device);
3564 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3566 hr = IDirect3DDevice7_GetRenderState(device, D3DRENDERSTATE_LIGHTING, &rs);
3567 ok(SUCCEEDED(hr), "Failed to get lighting render state, hr %#x.\n", hr);
3568 ok(rs == tests[i].d3drs_lighting, "D3DRENDERSTATE_LIGHTING is %#x, expected %#x.\n",
3569 rs, tests[i].d3drs_lighting);
3571 color = get_surface_color(rt, 320, 240);
3572 ok(compare_color(color, tests[i].color, 1),
3573 "Got unexpected color 0x%08x, expected 0x%08x, test %u.\n",
3574 color, tests[i].color, i);
3577 IDirectDrawSurface7_Release(rt);
3578 ref = IDirect3DDevice7_Release(device);
3579 ok(ref == 0, "Device not properly released, refcount %u.\n", ref);
3580 DestroyWindow(window);
3583 static struct
3585 BOOL received;
3586 IDirectDraw7 *ddraw;
3587 HWND window;
3588 DWORD coop_level;
3589 } activateapp_testdata;
3591 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
3593 if (message == WM_ACTIVATEAPP)
3595 if (activateapp_testdata.ddraw)
3597 HRESULT hr;
3598 activateapp_testdata.received = FALSE;
3599 hr = IDirectDraw7_SetCooperativeLevel(activateapp_testdata.ddraw,
3600 activateapp_testdata.window, activateapp_testdata.coop_level);
3601 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
3602 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
3604 activateapp_testdata.received = TRUE;
3607 return DefWindowProcA(hwnd, message, wparam, lparam);
3610 static void test_coop_level_activateapp(void)
3612 IDirectDraw7 *ddraw;
3613 HRESULT hr;
3614 HWND window;
3615 WNDCLASSA wc = {0};
3616 DDSURFACEDESC2 ddsd;
3617 IDirectDrawSurface7 *surface;
3619 ddraw = create_ddraw();
3620 ok(!!ddraw, "Failed to create a ddraw object.\n");
3622 wc.lpfnWndProc = activateapp_test_proc;
3623 wc.lpszClassName = "ddraw_test_wndproc_wc";
3624 ok(RegisterClassA(&wc), "Failed to register window class.\n");
3626 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
3627 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
3629 /* Exclusive with window already active. */
3630 SetActiveWindow(window);
3631 activateapp_testdata.received = FALSE;
3632 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3633 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3634 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
3635 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3636 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3638 /* Exclusive with window not active. */
3639 SetActiveWindow(NULL);
3640 activateapp_testdata.received = FALSE;
3641 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3642 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3643 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3644 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3645 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3647 /* Normal with window not active, then exclusive with the same window. */
3648 SetActiveWindow(NULL);
3649 activateapp_testdata.received = FALSE;
3650 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3651 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3652 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
3653 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3654 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3655 /* Except in the first SetCooperativeLevel call, Windows XP randomly does not send
3656 * WM_ACTIVATEAPP. Windows 7 sends the message reliably. Mark the XP behavior broken. */
3657 ok(activateapp_testdata.received || broken(!activateapp_testdata.received),
3658 "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3659 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3660 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3662 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
3663 SetActiveWindow(NULL);
3664 activateapp_testdata.received = FALSE;
3665 activateapp_testdata.ddraw = ddraw;
3666 activateapp_testdata.window = window;
3667 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
3668 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3669 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3670 ok(activateapp_testdata.received || broken(!activateapp_testdata.received),
3671 "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3672 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3673 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3675 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
3676 * succeeding. Another switch to exclusive and back to normal is needed to release the
3677 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
3678 * WM_ACTIVATEAPP messages. */
3679 activateapp_testdata.ddraw = NULL;
3680 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3681 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3682 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3683 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3685 /* Setting DDSCL_NORMAL with recursive invocation. */
3686 SetActiveWindow(NULL);
3687 activateapp_testdata.received = FALSE;
3688 activateapp_testdata.ddraw = ddraw;
3689 activateapp_testdata.window = window;
3690 activateapp_testdata.coop_level = DDSCL_NORMAL;
3691 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3692 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3693 ok(activateapp_testdata.received || broken(!activateapp_testdata.received),
3694 "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3696 /* DDraw is in exlusive mode now. */
3697 memset(&ddsd, 0, sizeof(ddsd));
3698 ddsd.dwSize = sizeof(ddsd);
3699 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
3700 ddsd.dwBackBufferCount = 1;
3701 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
3702 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
3703 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
3704 IDirectDrawSurface7_Release(surface);
3706 /* Recover again, just to be sure. */
3707 activateapp_testdata.ddraw = NULL;
3708 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3709 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3710 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3711 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3713 DestroyWindow(window);
3714 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3715 IDirectDraw7_Release(ddraw);
3718 static void test_texturemanage(void)
3720 IDirectDraw7 *ddraw;
3721 HRESULT hr;
3722 DDSURFACEDESC2 ddsd;
3723 IDirectDrawSurface7 *surface;
3724 unsigned int i;
3725 DDCAPS hal_caps, hel_caps;
3726 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
3727 static const struct
3729 DWORD caps_in, caps2_in;
3730 HRESULT hr;
3731 DWORD caps_out, caps2_out;
3733 tests[] =
3735 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
3736 ~0U, ~0U},
3737 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
3738 ~0U, ~0U},
3739 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
3740 ~0U, ~0U},
3741 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
3742 ~0U, ~0U},
3743 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DD_OK,
3744 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE},
3745 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DD_OK,
3746 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE},
3747 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
3748 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_LOCALVIDMEM, 0},
3749 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
3750 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0},
3752 {0, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
3753 ~0U, ~0U},
3754 {0, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
3755 ~0U, ~0U},
3756 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
3757 ~0U, ~0U},
3758 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
3759 ~0U, ~0U},
3760 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
3761 ~0U, ~0U},
3762 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
3763 ~0U, ~0U},
3764 {DDSCAPS_VIDEOMEMORY, 0, DD_OK,
3765 DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY, 0},
3766 {DDSCAPS_SYSTEMMEMORY, 0, DD_OK,
3767 DDSCAPS_SYSTEMMEMORY, 0},
3770 ddraw = create_ddraw();
3771 ok(!!ddraw, "Failed to create a ddraw object.\n");
3772 hr = IDirectDraw7_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3773 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3775 memset(&hal_caps, 0, sizeof(hal_caps));
3776 hal_caps.dwSize = sizeof(hal_caps);
3777 memset(&hel_caps, 0, sizeof(hel_caps));
3778 hel_caps.dwSize = sizeof(hel_caps);
3779 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, &hel_caps);
3780 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
3781 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps)
3783 skip("Managed textures not supported, skipping managed texture test.\n");
3784 IDirectDraw7_Release(ddraw);
3785 return;
3788 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
3790 memset(&ddsd, 0, sizeof(ddsd));
3791 ddsd.dwSize = sizeof(ddsd);
3792 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3793 ddsd.ddsCaps.dwCaps = tests[i].caps_in;
3794 ddsd.ddsCaps.dwCaps2 = tests[i].caps2_in;
3795 ddsd.dwWidth = 4;
3796 ddsd.dwHeight = 4;
3798 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
3799 ok(hr == tests[i].hr, "Got unexpected, hr %#x, case %u.\n", hr, i);
3800 if (FAILED(hr))
3801 continue;
3803 memset(&ddsd, 0, sizeof(ddsd));
3804 ddsd.dwSize = sizeof(ddsd);
3805 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
3806 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3808 ok(ddsd.ddsCaps.dwCaps == tests[i].caps_out,
3809 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
3810 tests[i].caps_in, tests[i].caps2_in, tests[i].caps_out, ddsd.ddsCaps.dwCaps, i);
3811 ok(ddsd.ddsCaps.dwCaps2 == tests[i].caps2_out,
3812 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
3813 tests[i].caps_in, tests[i].caps2_in, tests[i].caps2_out, ddsd.ddsCaps.dwCaps2, i);
3815 IDirectDrawSurface7_Release(surface);
3818 IDirectDraw7_Release(ddraw);
3821 #define SUPPORT_DXT1 0x01
3822 #define SUPPORT_DXT2 0x02
3823 #define SUPPORT_DXT3 0x04
3824 #define SUPPORT_DXT4 0x08
3825 #define SUPPORT_DXT5 0x10
3826 #define SUPPORT_YUY2 0x20
3827 #define SUPPORT_UYVY 0x40
3829 static HRESULT WINAPI test_block_formats_creation_cb(DDPIXELFORMAT *fmt, void *ctx)
3831 DWORD *supported_fmts = ctx;
3833 if (!(fmt->dwFlags & DDPF_FOURCC))
3834 return DDENUMRET_OK;
3836 switch (fmt->dwFourCC)
3838 case MAKEFOURCC('D','X','T','1'):
3839 *supported_fmts |= SUPPORT_DXT1;
3840 break;
3841 case MAKEFOURCC('D','X','T','2'):
3842 *supported_fmts |= SUPPORT_DXT2;
3843 break;
3844 case MAKEFOURCC('D','X','T','3'):
3845 *supported_fmts |= SUPPORT_DXT3;
3846 break;
3847 case MAKEFOURCC('D','X','T','4'):
3848 *supported_fmts |= SUPPORT_DXT4;
3849 break;
3850 case MAKEFOURCC('D','X','T','5'):
3851 *supported_fmts |= SUPPORT_DXT5;
3852 break;
3853 case MAKEFOURCC('Y','U','Y','2'):
3854 *supported_fmts |= SUPPORT_YUY2;
3855 break;
3856 case MAKEFOURCC('U','Y','V','Y'):
3857 *supported_fmts |= SUPPORT_UYVY;
3858 break;
3859 default:
3860 break;
3863 return DDENUMRET_OK;
3866 static void test_block_formats_creation(void)
3868 HRESULT hr, expect_hr;
3869 unsigned int i, j, w, h;
3870 HWND window;
3871 IDirectDraw7 *ddraw;
3872 IDirect3D7 *d3d;
3873 IDirect3DDevice7 *device;
3874 IDirectDrawSurface7 *surface;
3875 DWORD supported_fmts = 0, supported_overlay_fmts = 0;
3876 DWORD num_fourcc_codes = 0, *fourcc_codes;
3877 DDSURFACEDESC2 ddsd;
3878 DDCAPS hal_caps;
3879 void *mem;
3881 static const struct
3883 DWORD fourcc;
3884 const char *name;
3885 DWORD support_flag;
3886 unsigned int block_width;
3887 unsigned int block_height;
3888 unsigned int block_size;
3889 BOOL create_size_checked, overlay;
3891 formats[] =
3893 {MAKEFOURCC('D','X','T','1'), "D3DFMT_DXT1", SUPPORT_DXT1, 4, 4, 8, TRUE, FALSE},
3894 {MAKEFOURCC('D','X','T','2'), "D3DFMT_DXT2", SUPPORT_DXT2, 4, 4, 16, TRUE, FALSE},
3895 {MAKEFOURCC('D','X','T','3'), "D3DFMT_DXT3", SUPPORT_DXT3, 4, 4, 16, TRUE, FALSE},
3896 {MAKEFOURCC('D','X','T','4'), "D3DFMT_DXT4", SUPPORT_DXT4, 4, 4, 16, TRUE, FALSE},
3897 {MAKEFOURCC('D','X','T','5'), "D3DFMT_DXT5", SUPPORT_DXT5, 4, 4, 16, TRUE, FALSE},
3898 {MAKEFOURCC('Y','U','Y','2'), "D3DFMT_YUY2", SUPPORT_YUY2, 2, 1, 4, FALSE, TRUE },
3899 {MAKEFOURCC('U','Y','V','Y'), "D3DFMT_UYVY", SUPPORT_UYVY, 2, 1, 4, FALSE, TRUE },
3901 static const struct
3903 DWORD caps, caps2;
3904 const char *name;
3905 BOOL overlay;
3907 types[] =
3909 /* DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY fails to create any fourcc
3910 * surface with DDERR_INVALIDPIXELFORMAT. Don't care about it for now.
3912 * Nvidia returns E_FAIL on DXTN DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY.
3913 * Other hw / drivers successfully create those surfaces. Ignore them, this
3914 * suggests that no game uses this, otherwise Nvidia would support it. */
3916 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0,
3917 "videomemory texture", FALSE
3920 DDSCAPS_VIDEOMEMORY | DDSCAPS_OVERLAY, 0,
3921 "videomemory overlay", TRUE
3924 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0,
3925 "systemmemory texture", FALSE
3928 DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE,
3929 "managed texture", FALSE
3932 enum size_type
3934 SIZE_TYPE_ZERO,
3935 SIZE_TYPE_PITCH,
3936 SIZE_TYPE_SIZE,
3938 static const struct
3940 DWORD flags;
3941 enum size_type size_type;
3942 int rel_size;
3943 HRESULT hr;
3945 user_mem_tests[] =
3947 {DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DD_OK},
3948 {DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
3949 {DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
3950 {DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
3951 {DDSD_LPSURFACE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
3952 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
3953 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_PITCH, 0, DDERR_INVALIDPARAMS},
3954 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
3955 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 1, DD_OK},
3956 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, -1, DDERR_INVALIDPARAMS},
3957 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
3958 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_PITCH, 0, DDERR_INVALIDPARAMS},
3959 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_SIZE, 0, DDERR_INVALIDPARAMS},
3960 {DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DDERR_INVALIDPARAMS},
3963 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3964 0, 0, 640, 480, 0, 0, 0, 0);
3966 if (!(device = create_device(window, DDSCL_NORMAL)))
3968 skip("Failed to create a 3D device, skipping test.\n");
3969 DestroyWindow(window);
3970 return;
3973 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
3974 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
3975 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **) &ddraw);
3976 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
3977 IDirect3D7_Release(d3d);
3979 hr = IDirect3DDevice7_EnumTextureFormats(device, test_block_formats_creation_cb,
3980 &supported_fmts);
3981 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
3983 hr = IDirectDraw7_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
3984 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
3985 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3986 num_fourcc_codes * sizeof(*fourcc_codes));
3987 if (!fourcc_codes)
3988 goto cleanup;
3989 hr = IDirectDraw7_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
3990 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
3991 for (i = 0; i < num_fourcc_codes; i++)
3993 for (j = 0; j < sizeof(formats) / sizeof(*formats); j++)
3995 if (fourcc_codes[i] == formats[j].fourcc)
3996 supported_overlay_fmts |= formats[j].support_flag;
3999 HeapFree(GetProcessHeap(), 0, fourcc_codes);
4001 memset(&hal_caps, 0, sizeof(hal_caps));
4002 hal_caps.dwSize = sizeof(hal_caps);
4003 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
4004 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4006 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2 * 2 * 16 + 1);
4008 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
4010 for (j = 0; j < sizeof(types) / sizeof(*types); j++)
4012 BOOL support;
4014 if (formats[i].overlay != types[j].overlay
4015 || (types[j].overlay && !(hal_caps.dwCaps & DDCAPS_OVERLAY)))
4016 continue;
4018 if (formats[i].overlay)
4019 support = supported_overlay_fmts & formats[i].support_flag;
4020 else
4021 support = supported_fmts & formats[i].support_flag;
4023 for (w = 1; w <= 8; w++)
4025 for (h = 1; h <= 8; h++)
4027 BOOL block_aligned = TRUE;
4028 BOOL todo = FALSE;
4030 if (w & (formats[i].block_width - 1) || h & (formats[i].block_height - 1))
4031 block_aligned = FALSE;
4033 memset(&ddsd, 0, sizeof(ddsd));
4034 ddsd.dwSize = sizeof(ddsd);
4035 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4036 ddsd.ddsCaps.dwCaps = types[j].caps;
4037 ddsd.ddsCaps.dwCaps2 = types[j].caps2;
4038 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
4039 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
4040 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
4041 ddsd.dwWidth = w;
4042 ddsd.dwHeight = h;
4044 /* TODO: Handle power of two limitations. I cannot test the pow2
4045 * behavior on windows because I have no hardware that doesn't at
4046 * least support np2_conditional. There's probably no HW that
4047 * supports DXTN textures but no conditional np2 textures. */
4048 if (!support && !(types[j].caps & DDSCAPS_SYSTEMMEMORY))
4049 expect_hr = DDERR_INVALIDPARAMS;
4050 else if (formats[i].create_size_checked && !block_aligned)
4052 expect_hr = DDERR_INVALIDPARAMS;
4053 if (!(types[j].caps & DDSCAPS_TEXTURE))
4054 todo = TRUE;
4056 else
4057 expect_hr = D3D_OK;
4059 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
4060 if (todo)
4061 todo_wine ok(hr == expect_hr,
4062 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
4063 hr, formats[i].name, types[j].name, w, h, expect_hr);
4064 else
4065 ok(hr == expect_hr,
4066 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
4067 hr, formats[i].name, types[j].name, w, h, expect_hr);
4069 if (SUCCEEDED(hr))
4070 IDirectDrawSurface7_Release(surface);
4075 if (formats[i].overlay)
4076 continue;
4078 for (j = 0; j < sizeof(user_mem_tests) / sizeof(*user_mem_tests); ++j)
4080 memset(&ddsd, 0, sizeof(ddsd));
4081 ddsd.dwSize = sizeof(ddsd);
4082 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | user_mem_tests[j].flags;
4083 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE;
4085 switch (user_mem_tests[j].size_type)
4087 case SIZE_TYPE_ZERO:
4088 U1(ddsd).dwLinearSize = 0;
4089 break;
4091 case SIZE_TYPE_PITCH:
4092 U1(ddsd).dwLinearSize = 2 * formats[i].block_size;
4093 break;
4095 case SIZE_TYPE_SIZE:
4096 U1(ddsd).dwLinearSize = 2 * 2 * formats[i].block_size;
4097 break;
4099 U1(ddsd).dwLinearSize += user_mem_tests[j].rel_size;
4101 ddsd.lpSurface = mem;
4102 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
4103 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
4104 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
4105 ddsd.dwWidth = 8;
4106 ddsd.dwHeight = 8;
4108 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
4109 ok(hr == user_mem_tests[j].hr, "Test %u: Got unexpected hr %#x, format %s.\n", j, hr, formats[i].name);
4111 if (FAILED(hr))
4112 continue;
4114 memset(&ddsd, 0, sizeof(ddsd));
4115 ddsd.dwSize = sizeof(ddsd);
4116 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
4117 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", j, hr);
4118 ok(ddsd.dwFlags == (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_LINEARSIZE),
4119 "Test %u: Got unexpected flags %#x.\n", j, ddsd.dwFlags);
4120 if (user_mem_tests[j].flags & DDSD_LPSURFACE)
4121 ok(U1(ddsd).dwLinearSize == ~0u, "Test %u: Got unexpected linear size %#x.\n",
4122 j, U1(ddsd).dwLinearSize);
4123 else
4124 ok(U1(ddsd).dwLinearSize == 2 * 2 * formats[i].block_size,
4125 "Test %u: Got unexpected linear size %#x, expected %#x.\n",
4126 j, U1(ddsd).dwLinearSize, 2 * 2 * formats[i].block_size);
4127 IDirectDrawSurface7_Release(surface);
4131 HeapFree(GetProcessHeap(), 0, mem);
4132 cleanup:
4133 IDirectDraw7_Release(ddraw);
4134 IDirect3DDevice7_Release(device);
4135 DestroyWindow(window);
4138 struct format_support_check
4140 const DDPIXELFORMAT *format;
4141 BOOL supported;
4144 static HRESULT WINAPI test_unsupported_formats_cb(DDPIXELFORMAT *fmt, void *ctx)
4146 struct format_support_check *format = ctx;
4148 if (!memcmp(format->format, fmt, sizeof(*fmt)))
4150 format->supported = TRUE;
4151 return DDENUMRET_CANCEL;
4154 return DDENUMRET_OK;
4157 static void test_unsupported_formats(void)
4159 HRESULT hr;
4160 BOOL expect_success;
4161 HWND window;
4162 IDirectDraw7 *ddraw;
4163 IDirect3D7 *d3d;
4164 IDirect3DDevice7 *device;
4165 IDirectDrawSurface7 *surface;
4166 DDSURFACEDESC2 ddsd;
4167 unsigned int i, j;
4168 DWORD expected_caps;
4169 static const struct
4171 const char *name;
4172 DDPIXELFORMAT fmt;
4174 formats[] =
4177 "D3DFMT_A8R8G8B8",
4179 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
4180 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
4184 "D3DFMT_P8",
4186 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
4187 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
4191 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
4193 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4194 0, 0, 640, 480, 0, 0, 0, 0);
4196 if (!(device = create_device(window, DDSCL_NORMAL)))
4198 skip("Failed to create a 3D device, skipping test.\n");
4199 DestroyWindow(window);
4200 return;
4203 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
4204 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
4205 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **) &ddraw);
4206 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
4207 IDirect3D7_Release(d3d);
4209 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
4211 struct format_support_check check = {&formats[i].fmt, FALSE};
4212 hr = IDirect3DDevice7_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
4213 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
4215 for (j = 0; j < sizeof(caps) / sizeof(*caps); j++)
4217 memset(&ddsd, 0, sizeof(ddsd));
4218 ddsd.dwSize = sizeof(ddsd);
4219 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
4220 U4(ddsd).ddpfPixelFormat = formats[i].fmt;
4221 ddsd.dwWidth = 4;
4222 ddsd.dwHeight = 4;
4223 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
4225 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
4226 expect_success = FALSE;
4227 else
4228 expect_success = TRUE;
4230 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
4231 ok(SUCCEEDED(hr) == expect_success,
4232 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
4233 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
4234 if (FAILED(hr))
4235 continue;
4237 memset(&ddsd, 0, sizeof(ddsd));
4238 ddsd.dwSize = sizeof(ddsd);
4239 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
4240 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4242 if (caps[j] & DDSCAPS_VIDEOMEMORY)
4243 expected_caps = DDSCAPS_VIDEOMEMORY;
4244 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
4245 expected_caps = DDSCAPS_SYSTEMMEMORY;
4246 else if (check.supported)
4247 expected_caps = DDSCAPS_VIDEOMEMORY;
4248 else
4249 expected_caps = DDSCAPS_SYSTEMMEMORY;
4251 ok(ddsd.ddsCaps.dwCaps & expected_caps,
4252 "Expected capability %#x, format %s, input cap %#x.\n",
4253 expected_caps, formats[i].name, caps[j]);
4255 IDirectDrawSurface7_Release(surface);
4259 IDirectDraw7_Release(ddraw);
4260 IDirect3DDevice7_Release(device);
4261 DestroyWindow(window);
4264 static void test_rt_caps(void)
4266 const GUID *devtype = &IID_IDirect3DHALDevice;
4267 PALETTEENTRY palette_entries[256];
4268 IDirectDrawPalette *palette;
4269 IDirectDraw7 *ddraw;
4270 BOOL hal_ok = FALSE;
4271 DDPIXELFORMAT z_fmt;
4272 IDirect3D7 *d3d;
4273 unsigned int i;
4274 ULONG refcount;
4275 HWND window;
4276 HRESULT hr;
4278 static const DDPIXELFORMAT p8_fmt =
4280 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
4281 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
4284 const struct
4286 const DDPIXELFORMAT *pf;
4287 DWORD caps_in;
4288 DWORD caps_out;
4289 HRESULT create_device_hr;
4290 HRESULT set_rt_hr, alternative_set_rt_hr;
4292 test_data[] =
4295 NULL,
4296 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
4297 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4298 D3D_OK,
4299 D3D_OK,
4300 D3D_OK,
4303 NULL,
4304 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4305 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4306 D3D_OK,
4307 D3D_OK,
4308 D3D_OK,
4311 NULL,
4312 DDSCAPS_OFFSCREENPLAIN,
4313 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4314 DDERR_INVALIDCAPS,
4315 DDERR_INVALIDCAPS,
4316 DDERR_INVALIDCAPS,
4319 NULL,
4320 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4321 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4322 D3DERR_SURFACENOTINVIDMEM,
4323 DDERR_INVALIDPARAMS,
4324 D3D_OK,
4327 NULL,
4328 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4329 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4330 DDERR_INVALIDCAPS,
4331 DDERR_INVALIDCAPS,
4332 DDERR_INVALIDCAPS,
4335 NULL,
4336 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
4337 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4338 D3D_OK,
4339 D3D_OK,
4340 D3D_OK,
4343 NULL,
4344 DDSCAPS_3DDEVICE,
4345 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4346 D3D_OK,
4347 D3D_OK,
4348 D3D_OK,
4351 NULL,
4353 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4354 DDERR_INVALIDCAPS,
4355 DDERR_INVALIDCAPS,
4356 DDERR_INVALIDCAPS,
4359 NULL,
4360 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4361 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4362 D3DERR_SURFACENOTINVIDMEM,
4363 DDERR_INVALIDPARAMS,
4364 D3D_OK,
4367 NULL,
4368 DDSCAPS_SYSTEMMEMORY,
4369 DDSCAPS_SYSTEMMEMORY,
4370 DDERR_INVALIDCAPS,
4371 DDERR_INVALIDCAPS,
4372 DDERR_INVALIDCAPS,
4375 &p8_fmt,
4377 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4378 DDERR_INVALIDCAPS,
4379 DDERR_INVALIDCAPS,
4380 DDERR_INVALIDCAPS,
4383 &p8_fmt,
4384 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4385 ~0U /* AMD r200 */,
4386 DDERR_NOPALETTEATTACHED,
4387 DDERR_INVALIDCAPS,
4388 DDERR_INVALIDCAPS,
4391 &p8_fmt,
4392 DDSCAPS_OFFSCREENPLAIN,
4393 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4394 DDERR_INVALIDCAPS,
4395 DDERR_INVALIDCAPS,
4396 DDERR_INVALIDCAPS,
4399 &p8_fmt,
4400 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4401 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4402 DDERR_NOPALETTEATTACHED,
4403 DDERR_INVALIDCAPS,
4404 DDERR_INVALIDCAPS,
4407 &p8_fmt,
4408 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4409 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4410 DDERR_INVALIDCAPS,
4411 DDERR_INVALIDCAPS,
4412 DDERR_INVALIDCAPS,
4415 &z_fmt,
4416 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
4417 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4418 DDERR_INVALIDCAPS,
4419 DDERR_INVALIDPIXELFORMAT,
4420 DDERR_INVALIDPIXELFORMAT,
4423 &z_fmt,
4424 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4425 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4426 DDERR_INVALIDCAPS,
4427 DDERR_INVALIDPIXELFORMAT,
4428 DDERR_INVALIDPIXELFORMAT,
4431 &z_fmt,
4432 DDSCAPS_ZBUFFER,
4433 DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4434 DDERR_INVALIDCAPS,
4435 DDERR_INVALIDCAPS,
4436 DDERR_INVALIDCAPS,
4439 &z_fmt,
4440 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4441 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4442 DDERR_INVALIDCAPS,
4443 DDERR_INVALIDPARAMS,
4444 DDERR_INVALIDPIXELFORMAT,
4447 &z_fmt,
4448 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
4449 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
4450 DDERR_INVALIDCAPS,
4451 DDERR_INVALIDCAPS,
4452 DDERR_INVALIDCAPS,
4456 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4457 0, 0, 640, 480, 0, 0, 0, 0);
4458 ddraw = create_ddraw();
4459 ok(!!ddraw, "Failed to create a ddraw object.\n");
4460 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4461 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4463 if (FAILED(IDirectDraw7_QueryInterface(ddraw, &IID_IDirect3D7, (void **)&d3d)))
4465 skip("D3D interface is not available, skipping test.\n");
4466 goto done;
4469 hr = IDirect3D7_EnumDevices(d3d, enum_devtype_cb, &hal_ok);
4470 ok(SUCCEEDED(hr), "Failed to enumerate devices, hr %#x.\n", hr);
4471 if (hal_ok)
4472 devtype = &IID_IDirect3DTnLHalDevice;
4474 memset(&z_fmt, 0, sizeof(z_fmt));
4475 hr = IDirect3D7_EnumZBufferFormats(d3d, devtype, enum_z_fmt, &z_fmt);
4476 if (FAILED(hr) || !z_fmt.dwSize)
4478 skip("No depth buffer formats available, skipping test.\n");
4479 IDirect3D7_Release(d3d);
4480 goto done;
4483 memset(palette_entries, 0, sizeof(palette_entries));
4484 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
4485 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
4487 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
4489 IDirectDrawSurface7 *surface, *rt, *expected_rt, *tmp;
4490 DDSURFACEDESC2 surface_desc;
4491 IDirect3DDevice7 *device;
4493 memset(&surface_desc, 0, sizeof(surface_desc));
4494 surface_desc.dwSize = sizeof(surface_desc);
4495 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4496 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
4497 if (test_data[i].pf)
4499 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
4500 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
4502 surface_desc.dwWidth = 640;
4503 surface_desc.dwHeight = 480;
4504 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4505 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
4506 i, test_data[i].caps_in, hr);
4508 memset(&surface_desc, 0, sizeof(surface_desc));
4509 surface_desc.dwSize = sizeof(surface_desc);
4510 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &surface_desc);
4511 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
4512 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
4513 "Test %u: Got unexpected caps %#x, expected %#x.\n",
4514 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
4516 hr = IDirect3D7_CreateDevice(d3d, devtype, surface, &device);
4517 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
4518 i, hr, test_data[i].create_device_hr);
4519 if (FAILED(hr))
4521 if (hr == DDERR_NOPALETTEATTACHED)
4523 hr = IDirectDrawSurface7_SetPalette(surface, palette);
4524 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
4525 hr = IDirect3D7_CreateDevice(d3d, devtype, surface, &device);
4526 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
4527 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
4528 else
4529 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
4531 IDirectDrawSurface7_Release(surface);
4533 memset(&surface_desc, 0, sizeof(surface_desc));
4534 surface_desc.dwSize = sizeof(surface_desc);
4535 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4536 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
4537 surface_desc.dwWidth = 640;
4538 surface_desc.dwHeight = 480;
4539 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4540 ok(SUCCEEDED(hr), "Test %u: Failed to create surface, hr %#x.\n", i, hr);
4542 hr = IDirect3D7_CreateDevice(d3d, devtype, surface, &device);
4543 ok(SUCCEEDED(hr), "Test %u: Failed to create device, hr %#x.\n", i, hr);
4546 memset(&surface_desc, 0, sizeof(surface_desc));
4547 surface_desc.dwSize = sizeof(surface_desc);
4548 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4549 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
4550 if (test_data[i].pf)
4552 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
4553 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
4555 surface_desc.dwWidth = 640;
4556 surface_desc.dwHeight = 480;
4557 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &rt, NULL);
4558 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
4559 i, test_data[i].caps_in, hr);
4561 hr = IDirect3DDevice7_SetRenderTarget(device, rt, 0);
4562 ok(hr == test_data[i].set_rt_hr || broken(hr == test_data[i].alternative_set_rt_hr),
4563 "Test %u: Got unexpected hr %#x, expected %#x.\n",
4564 i, hr, test_data[i].set_rt_hr);
4565 if (SUCCEEDED(hr) || hr == DDERR_INVALIDPIXELFORMAT)
4566 expected_rt = rt;
4567 else
4568 expected_rt = surface;
4570 hr = IDirect3DDevice7_GetRenderTarget(device, &tmp);
4571 ok(SUCCEEDED(hr), "Test %u: Failed to get render target, hr %#x.\n", i, hr);
4572 ok(tmp == expected_rt, "Test %u: Got unexpected rt %p.\n", i, tmp);
4574 IDirectDrawSurface7_Release(tmp);
4575 IDirectDrawSurface7_Release(rt);
4576 refcount = IDirect3DDevice7_Release(device);
4577 ok(refcount == 0, "Test %u: The device was not properly freed, refcount %u.\n", i, refcount);
4578 refcount = IDirectDrawSurface7_Release(surface);
4579 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
4582 IDirectDrawPalette_Release(palette);
4583 IDirect3D7_Release(d3d);
4585 done:
4586 refcount = IDirectDraw7_Release(ddraw);
4587 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4588 DestroyWindow(window);
4591 static void test_primary_caps(void)
4593 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
4594 IDirectDrawSurface7 *surface;
4595 DDSURFACEDESC2 surface_desc;
4596 IDirectDraw7 *ddraw;
4597 unsigned int i;
4598 ULONG refcount;
4599 HWND window;
4600 HRESULT hr;
4602 static const struct
4604 DWORD coop_level;
4605 DWORD caps_in;
4606 DWORD back_buffer_count;
4607 HRESULT hr;
4608 DWORD caps_out;
4610 test_data[] =
4613 DDSCL_NORMAL,
4614 DDSCAPS_PRIMARYSURFACE,
4615 ~0u,
4616 DD_OK,
4617 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
4620 DDSCL_NORMAL,
4621 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
4622 ~0u,
4623 DDERR_INVALIDCAPS,
4624 ~0u,
4627 DDSCL_NORMAL,
4628 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
4629 ~0u,
4630 DDERR_INVALIDCAPS,
4631 ~0u,
4634 DDSCL_NORMAL,
4635 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
4636 ~0u,
4637 DDERR_INVALIDCAPS,
4638 ~0u,
4641 DDSCL_NORMAL,
4642 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
4643 ~0u,
4644 DDERR_INVALIDCAPS,
4645 ~0u,
4648 DDSCL_NORMAL,
4649 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
4650 ~0u,
4651 DDERR_INVALIDCAPS,
4652 ~0u,
4655 DDSCL_NORMAL,
4656 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4657 ~0u,
4658 DDERR_INVALIDCAPS,
4659 ~0u,
4662 DDSCL_NORMAL,
4663 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4665 DDERR_INVALIDCAPS,
4666 ~0u,
4669 DDSCL_NORMAL,
4670 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4672 DDERR_NOEXCLUSIVEMODE,
4673 ~0u,
4676 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4677 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4679 DDERR_INVALIDCAPS,
4680 ~0u,
4683 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4684 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
4686 DD_OK,
4687 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
4690 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4691 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
4693 DDERR_INVALIDCAPS,
4694 ~0u,
4697 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
4698 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
4700 DDERR_INVALIDCAPS,
4701 ~0u,
4705 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4706 0, 0, 640, 480, 0, 0, 0, 0);
4707 ddraw = create_ddraw();
4708 ok(!!ddraw, "Failed to create a ddraw object.\n");
4710 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
4712 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
4713 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4715 memset(&surface_desc, 0, sizeof(surface_desc));
4716 surface_desc.dwSize = sizeof(surface_desc);
4717 surface_desc.dwFlags = DDSD_CAPS;
4718 if (test_data[i].back_buffer_count != ~0u)
4719 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
4720 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
4721 surface_desc.dwBackBufferCount = test_data[i].back_buffer_count;
4722 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4723 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
4724 if (FAILED(hr))
4725 continue;
4727 memset(&surface_desc, 0, sizeof(surface_desc));
4728 surface_desc.dwSize = sizeof(surface_desc);
4729 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &surface_desc);
4730 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
4731 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
4732 "Test %u: Got unexpected caps %#x, expected %#x.\n",
4733 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
4735 IDirectDrawSurface7_Release(surface);
4738 refcount = IDirectDraw7_Release(ddraw);
4739 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4740 DestroyWindow(window);
4743 static void test_surface_lock(void)
4745 IDirectDraw7 *ddraw;
4746 IDirect3D7 *d3d = NULL;
4747 IDirectDrawSurface7 *surface;
4748 IDirect3DDevice7 *device;
4749 HRESULT hr;
4750 HWND window;
4751 unsigned int i;
4752 DDSURFACEDESC2 ddsd;
4753 ULONG refcount;
4754 DDPIXELFORMAT z_fmt;
4755 BOOL hal_ok = FALSE;
4756 const GUID *devtype = &IID_IDirect3DHALDevice;
4757 D3DDEVICEDESC7 device_desc;
4758 BOOL cubemap_supported;
4759 static const struct
4761 DWORD caps;
4762 DWORD caps2;
4763 const char *name;
4765 tests[] =
4768 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
4770 "videomemory offscreenplain"
4773 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4775 "systemmemory offscreenplain"
4778 DDSCAPS_PRIMARYSURFACE,
4780 "primary"
4783 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
4785 "videomemory texture"
4788 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
4789 DDSCAPS2_OPAQUE,
4790 "opaque videomemory texture"
4793 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
4795 "systemmemory texture"
4798 DDSCAPS_TEXTURE,
4799 DDSCAPS2_TEXTUREMANAGE,
4800 "managed texture"
4803 DDSCAPS_TEXTURE,
4804 DDSCAPS2_D3DTEXTUREMANAGE,
4805 "managed texture"
4808 DDSCAPS_TEXTURE,
4809 DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_OPAQUE,
4810 "opaque managed texture"
4813 DDSCAPS_TEXTURE,
4814 DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_OPAQUE,
4815 "opaque managed texture"
4818 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4820 "render target"
4823 DDSCAPS_ZBUFFER,
4825 "Z buffer"
4828 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_VIDEOMEMORY,
4829 DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES,
4830 "videomemory cube"
4833 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_VIDEOMEMORY,
4834 DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES | DDSCAPS2_OPAQUE,
4835 "opaque videomemory cube"
4838 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_SYSTEMMEMORY,
4839 DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES,
4840 "systemmemory cube"
4843 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX,
4844 DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES,
4845 "managed cube"
4848 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX,
4849 DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES,
4850 "managed cube"
4853 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX,
4854 DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES | DDSCAPS2_OPAQUE,
4855 "opaque managed cube"
4858 DDSCAPS_TEXTURE | DDSCAPS_COMPLEX,
4859 DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_ALLFACES | DDSCAPS2_OPAQUE,
4860 "opaque managed cube"
4864 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4865 0, 0, 640, 480, 0, 0, 0, 0);
4866 ddraw = create_ddraw();
4867 ok(!!ddraw, "Failed to create a ddraw object.\n");
4868 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4869 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4871 if (FAILED(IDirectDraw7_QueryInterface(ddraw, &IID_IDirect3D7, (void **)&d3d)))
4873 skip("D3D interface is not available, skipping test.\n");
4874 goto done;
4877 hr = IDirect3D7_EnumDevices(d3d, enum_devtype_cb, &hal_ok);
4878 ok(SUCCEEDED(hr), "Failed to enumerate devices, hr %#x.\n", hr);
4879 if (hal_ok)
4880 devtype = &IID_IDirect3DTnLHalDevice;
4882 memset(&z_fmt, 0, sizeof(z_fmt));
4883 hr = IDirect3D7_EnumZBufferFormats(d3d, devtype, enum_z_fmt, &z_fmt);
4884 if (FAILED(hr) || !z_fmt.dwSize)
4886 skip("No depth buffer formats available, skipping test.\n");
4887 goto done;
4890 memset(&ddsd, 0, sizeof(ddsd));
4891 ddsd.dwSize = sizeof(ddsd);
4892 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4893 ddsd.dwWidth = 64;
4894 ddsd.dwHeight = 64;
4895 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
4896 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
4897 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4899 hr = IDirect3D7_CreateDevice(d3d, devtype, surface, &device);
4900 ok(SUCCEEDED(hr), "Failed to create device, hr %#x.\n", hr);
4901 hr = IDirect3DDevice7_GetCaps(device, &device_desc);
4902 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
4903 cubemap_supported = !!(device_desc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_CUBEMAP);
4904 IDirect3DDevice7_Release(device);
4906 IDirectDrawSurface7_Release(surface);
4908 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4910 if (!cubemap_supported && tests[i].caps2 & DDSCAPS2_CUBEMAP)
4911 continue;
4913 memset(&ddsd, 0, sizeof(ddsd));
4914 ddsd.dwSize = sizeof(ddsd);
4915 ddsd.dwFlags = DDSD_CAPS;
4916 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
4918 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
4919 ddsd.dwWidth = 64;
4920 ddsd.dwHeight = 64;
4922 if (tests[i].caps & DDSCAPS_ZBUFFER)
4924 ddsd.dwFlags |= DDSD_PIXELFORMAT;
4925 U4(ddsd).ddpfPixelFormat = z_fmt;
4927 ddsd.ddsCaps.dwCaps = tests[i].caps;
4928 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
4930 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
4931 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
4933 memset(&ddsd, 0, sizeof(ddsd));
4934 ddsd.dwSize = sizeof(ddsd);
4935 hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4936 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
4937 if (SUCCEEDED(hr))
4939 hr = IDirectDrawSurface7_Unlock(surface, NULL);
4940 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
4943 IDirectDrawSurface7_Release(surface);
4946 done:
4947 if (d3d)
4948 IDirect3D7_Release(d3d);
4949 refcount = IDirectDraw7_Release(ddraw);
4950 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4951 DestroyWindow(window);
4954 static void test_surface_discard(void)
4956 IDirect3DDevice7 *device;
4957 IDirect3D7 *d3d;
4958 IDirectDraw7 *ddraw;
4959 HRESULT hr;
4960 HWND window;
4961 DDSURFACEDESC2 ddsd;
4962 IDirectDrawSurface7 *surface, *target;
4963 void *addr;
4964 static const struct
4966 DWORD caps, caps2;
4967 BOOL discard;
4969 tests[] =
4971 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0, TRUE},
4972 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
4973 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0, TRUE},
4974 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
4975 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE},
4976 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
4977 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE},
4978 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
4980 unsigned int i;
4982 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4983 0, 0, 640, 480, 0, 0, 0, 0);
4985 if (!(device = create_device(window, DDSCL_NORMAL)))
4987 skip("Failed to create a 3D device, skipping test.\n");
4988 DestroyWindow(window);
4989 return;
4991 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
4992 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
4993 hr = IDirect3D7_QueryInterface(d3d, &IID_IDirectDraw7, (void **)&ddraw);
4994 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
4995 hr = IDirect3DDevice7_GetRenderTarget(device, &target);
4996 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4998 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
5000 BOOL discarded;
5002 memset(&ddsd, 0, sizeof(ddsd));
5003 ddsd.dwSize = sizeof(ddsd);
5004 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5005 ddsd.ddsCaps.dwCaps = tests[i].caps;
5006 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
5007 ddsd.dwWidth = 64;
5008 ddsd.dwHeight = 64;
5009 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
5010 ok(SUCCEEDED(hr), "Failed to create offscreen surface, hr %#x, case %u.\n", hr, i);
5012 memset(&ddsd, 0, sizeof(ddsd));
5013 ddsd.dwSize = sizeof(ddsd);
5014 hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd, 0, NULL);
5015 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5016 addr = ddsd.lpSurface;
5017 hr = IDirectDrawSurface7_Unlock(surface, NULL);
5018 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5020 memset(&ddsd, 0, sizeof(ddsd));
5021 ddsd.dwSize = sizeof(ddsd);
5022 hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
5023 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5024 discarded = ddsd.lpSurface != addr;
5025 hr = IDirectDrawSurface7_Unlock(surface, NULL);
5026 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5028 hr = IDirectDrawSurface7_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
5029 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
5031 memset(&ddsd, 0, sizeof(ddsd));
5032 ddsd.dwSize = sizeof(ddsd);
5033 hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
5034 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5035 discarded |= ddsd.lpSurface != addr;
5036 hr = IDirectDrawSurface7_Unlock(surface, NULL);
5037 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5039 IDirectDrawSurface7_Release(surface);
5041 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
5042 * AMD r500, evergreen). Windows XP, at least on AMD r200, does not. */
5043 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
5046 IDirectDrawSurface7_Release(target);
5047 IDirectDraw7_Release(ddraw);
5048 IDirect3D7_Release(d3d);
5049 IDirect3DDevice7_Release(device);
5050 DestroyWindow(window);
5053 static void test_flip(void)
5055 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
5056 IDirectDrawSurface7 *primary, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
5057 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, 0};
5058 DDSURFACEDESC2 surface_desc;
5059 BOOL sysmem_primary;
5060 IDirectDraw7 *ddraw;
5061 D3DCOLOR color;
5062 ULONG refcount;
5063 HWND window;
5064 DDBLTFX fx;
5065 HRESULT hr;
5067 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5068 0, 0, 640, 480, 0, 0, 0, 0);
5069 ddraw = create_ddraw();
5070 ok(!!ddraw, "Failed to create a ddraw object.\n");
5072 hr = set_display_mode(ddraw, 640, 480);
5073 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
5074 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5075 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5077 memset(&surface_desc, 0, sizeof(surface_desc));
5078 surface_desc.dwSize = sizeof(surface_desc);
5079 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
5080 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
5081 surface_desc.dwBackBufferCount = 3;
5082 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &primary, NULL);
5083 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5085 memset(&surface_desc, 0, sizeof(surface_desc));
5086 surface_desc.dwSize = sizeof(surface_desc);
5087 hr = IDirectDrawSurface7_GetSurfaceDesc(primary, &surface_desc);
5088 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5089 ok((surface_desc.ddsCaps.dwCaps & ~placement)
5090 == (DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX),
5091 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5092 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
5094 hr = IDirectDrawSurface7_GetAttachedSurface(primary, &caps, &backbuffer1);
5095 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5096 memset(&surface_desc, 0, sizeof(surface_desc));
5097 surface_desc.dwSize = sizeof(surface_desc);
5098 hr = IDirectDrawSurface7_GetSurfaceDesc(backbuffer1, &surface_desc);
5099 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5100 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
5101 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_BACKBUFFER),
5102 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5104 hr = IDirectDrawSurface7_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
5105 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5106 memset(&surface_desc, 0, sizeof(surface_desc));
5107 surface_desc.dwSize = sizeof(surface_desc);
5108 hr = IDirectDrawSurface7_GetSurfaceDesc(backbuffer2, &surface_desc);
5109 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5110 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
5111 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
5112 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5114 hr = IDirectDrawSurface7_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
5115 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5116 memset(&surface_desc, 0, sizeof(surface_desc));
5117 surface_desc.dwSize = sizeof(surface_desc);
5118 hr = IDirectDrawSurface7_GetSurfaceDesc(backbuffer3, &surface_desc);
5119 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5120 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
5121 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
5122 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5124 hr = IDirectDrawSurface7_GetAttachedSurface(backbuffer3, &caps, &surface);
5125 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5126 ok(surface == primary, "Got unexpected surface %p, expected %p.\n", surface, primary);
5127 IDirectDrawSurface7_Release(surface);
5129 memset(&surface_desc, 0, sizeof(surface_desc));
5130 surface_desc.dwSize = sizeof(surface_desc);
5131 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5132 surface_desc.ddsCaps.dwCaps = 0;
5133 surface_desc.dwWidth = 640;
5134 surface_desc.dwHeight = 480;
5135 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5136 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5137 hr = IDirectDrawSurface7_Flip(primary, surface, DDFLIP_WAIT);
5138 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5139 IDirectDrawSurface7_Release(surface);
5141 hr = IDirectDrawSurface7_Flip(primary, primary, DDFLIP_WAIT);
5142 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5143 hr = IDirectDrawSurface7_Flip(backbuffer1, NULL, DDFLIP_WAIT);
5144 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5145 hr = IDirectDrawSurface7_Flip(backbuffer2, NULL, DDFLIP_WAIT);
5146 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5147 hr = IDirectDrawSurface7_Flip(backbuffer3, NULL, DDFLIP_WAIT);
5148 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5150 memset(&fx, 0, sizeof(fx));
5151 fx.dwSize = sizeof(fx);
5152 U5(fx).dwFillColor = 0xffff0000;
5153 hr = IDirectDrawSurface7_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5154 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5155 U5(fx).dwFillColor = 0xff00ff00;
5156 hr = IDirectDrawSurface7_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5157 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5158 U5(fx).dwFillColor = 0xff0000ff;
5159 hr = IDirectDrawSurface7_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5160 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5162 hr = IDirectDrawSurface7_Flip(primary, NULL, DDFLIP_WAIT);
5163 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5164 color = get_surface_color(backbuffer1, 320, 240);
5165 /* The testbot seems to just copy the contents of one surface to all the
5166 * others, instead of properly flipping. */
5167 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
5168 "Got unexpected color 0x%08x.\n", color);
5169 color = get_surface_color(backbuffer2, 320, 240);
5170 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
5171 U5(fx).dwFillColor = 0xffff0000;
5172 hr = IDirectDrawSurface7_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5173 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5175 hr = IDirectDrawSurface7_Flip(primary, NULL, DDFLIP_WAIT);
5176 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5177 color = get_surface_color(backbuffer1, 320, 240);
5178 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
5179 "Got unexpected color 0x%08x.\n", color);
5180 color = get_surface_color(backbuffer2, 320, 240);
5181 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5182 U5(fx).dwFillColor = 0xff00ff00;
5183 hr = IDirectDrawSurface7_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5184 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5186 hr = IDirectDrawSurface7_Flip(primary, NULL, DDFLIP_WAIT);
5187 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5188 color = get_surface_color(backbuffer1, 320, 240);
5189 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
5190 "Got unexpected color 0x%08x.\n", color);
5191 color = get_surface_color(backbuffer2, 320, 240);
5192 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
5193 U5(fx).dwFillColor = 0xff0000ff;
5194 hr = IDirectDrawSurface7_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5195 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5197 hr = IDirectDrawSurface7_Flip(primary, backbuffer1, DDFLIP_WAIT);
5198 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5199 color = get_surface_color(backbuffer2, 320, 240);
5200 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
5201 "Got unexpected color 0x%08x.\n", color);
5202 color = get_surface_color(backbuffer3, 320, 240);
5203 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
5204 U5(fx).dwFillColor = 0xffff0000;
5205 hr = IDirectDrawSurface7_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5206 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5208 hr = IDirectDrawSurface7_Flip(primary, backbuffer2, DDFLIP_WAIT);
5209 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5210 color = get_surface_color(backbuffer1, 320, 240);
5211 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5212 color = get_surface_color(backbuffer3, 320, 240);
5213 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
5214 "Got unexpected color 0x%08x.\n", color);
5215 U5(fx).dwFillColor = 0xff00ff00;
5216 hr = IDirectDrawSurface7_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5217 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5219 hr = IDirectDrawSurface7_Flip(primary, backbuffer3, DDFLIP_WAIT);
5220 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5221 color = get_surface_color(backbuffer1, 320, 240);
5222 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
5223 "Got unexpected color 0x%08x.\n", color);
5224 color = get_surface_color(backbuffer2, 320, 240);
5225 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
5227 IDirectDrawSurface7_Release(backbuffer3);
5228 IDirectDrawSurface7_Release(backbuffer2);
5229 IDirectDrawSurface7_Release(backbuffer1);
5230 IDirectDrawSurface7_Release(primary);
5231 refcount = IDirectDraw7_Release(ddraw);
5232 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5233 DestroyWindow(window);
5236 static void reset_ddsd(DDSURFACEDESC2 *ddsd)
5238 memset(ddsd, 0, sizeof(*ddsd));
5239 ddsd->dwSize = sizeof(*ddsd);
5242 static void test_set_surface_desc(void)
5244 IDirectDraw7 *ddraw;
5245 HWND window;
5246 HRESULT hr;
5247 DDSURFACEDESC2 ddsd;
5248 IDirectDrawSurface7 *surface;
5249 BYTE data[16*16*4];
5250 ULONG ref;
5251 unsigned int i;
5252 static const struct
5254 DWORD caps, caps2;
5255 BOOL supported;
5256 const char *name;
5258 invalid_caps_tests[] =
5260 {DDSCAPS_VIDEOMEMORY, 0, FALSE, "videomemory plain"},
5261 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, TRUE, "systemmemory texture"},
5262 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE, "managed texture"},
5263 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE, "managed texture"},
5264 {DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY, 0, FALSE, "systemmemory primary"},
5267 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5268 0, 0, 640, 480, 0, 0, 0, 0);
5269 ddraw = create_ddraw();
5270 ok(!!ddraw, "Failed to create a ddraw object.\n");
5271 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5272 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5274 reset_ddsd(&ddsd);
5275 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
5276 ddsd.dwWidth = 8;
5277 ddsd.dwHeight = 8;
5278 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5279 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5280 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5281 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5282 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5283 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5284 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5286 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
5287 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5289 reset_ddsd(&ddsd);
5290 ddsd.dwFlags = DDSD_LPSURFACE;
5291 ddsd.lpSurface = data;
5292 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5293 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5295 /* Redundantly setting the same lpSurface is not an error. */
5296 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5297 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5299 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
5300 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5301 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
5302 ok(ddsd.lpSurface == NULL, "lpSurface is %p, expected NULL.\n", ddsd.lpSurface);
5304 hr = IDirectDrawSurface7_Lock(surface, NULL, &ddsd, 0, NULL);
5305 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5306 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
5307 ok(ddsd.lpSurface == data, "lpSurface is %p, expected %p.\n", data, data);
5308 hr = IDirectDrawSurface7_Unlock(surface, NULL);
5309 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5311 reset_ddsd(&ddsd);
5312 ddsd.dwFlags = DDSD_LPSURFACE;
5313 ddsd.lpSurface = data;
5314 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 1);
5315 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with flags=1 returned %#x.\n", hr);
5317 ddsd.lpSurface = NULL;
5318 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5319 ok(hr == DDERR_INVALIDPARAMS, "Setting lpSurface=NULL returned %#x.\n", hr);
5321 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, NULL, 0);
5322 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with NULL desc returned %#x.\n", hr);
5324 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
5325 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5326 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
5327 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
5328 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
5330 /* Setting the caps is an error. This also means the original description cannot be reapplied. */
5331 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5332 ok(hr == DDERR_INVALIDPARAMS, "Setting the original desc returned %#x.\n", hr);
5334 ddsd.dwFlags = DDSD_CAPS;
5335 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5336 ok(hr == DDERR_INVALIDPARAMS, "Setting DDSD_CAPS returned %#x.\n", hr);
5338 /* dwCaps = 0 is allowed, but ignored. Caps2 can be anything and is ignored too. */
5339 ddsd.dwFlags = DDSD_CAPS | DDSD_LPSURFACE;
5340 ddsd.lpSurface = data;
5341 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5342 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
5343 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5344 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5345 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
5346 ddsd.ddsCaps.dwCaps = 0;
5347 ddsd.ddsCaps.dwCaps2 = 0xdeadbeef;
5348 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5349 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5351 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
5352 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5353 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
5354 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
5355 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
5357 /* Setting the height is allowed, but it cannot be set to 0, and only if LPSURFACE is set too. */
5358 reset_ddsd(&ddsd);
5359 ddsd.dwFlags = DDSD_HEIGHT;
5360 ddsd.dwHeight = 16;
5361 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5362 ok(hr == DDERR_INVALIDPARAMS, "Setting height without lpSurface returned %#x.\n", hr);
5364 ddsd.lpSurface = data;
5365 ddsd.dwFlags = DDSD_HEIGHT | DDSD_LPSURFACE;
5366 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5367 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5369 ddsd.dwHeight = 0;
5370 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5371 ok(hr == DDERR_INVALIDPARAMS, "Setting height=0 returned %#x.\n", hr);
5373 reset_ddsd(&ddsd);
5374 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
5375 ok(SUCCEEDED(hr), "GetSurfaceDesc failed, hr %#x.\n", hr);
5376 ok(ddsd.dwWidth == 8, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
5377 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
5379 /* Pitch and width can be set, but only together, and only with LPSURFACE. They must not be 0. */
5380 reset_ddsd(&ddsd);
5381 ddsd.dwFlags = DDSD_PITCH;
5382 U1(ddsd).lPitch = 8 * 4;
5383 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5384 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch without lpSurface or width returned %#x.\n", hr);
5386 ddsd.dwFlags = DDSD_WIDTH;
5387 ddsd.dwWidth = 16;
5388 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5389 ok(hr == DDERR_INVALIDPARAMS, "Setting width without lpSurface or pitch returned %#x.\n", hr);
5391 ddsd.dwFlags = DDSD_PITCH | DDSD_LPSURFACE;
5392 ddsd.lpSurface = data;
5393 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5394 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch and lpSurface without width returned %#x.\n", hr);
5396 ddsd.dwFlags = DDSD_WIDTH | DDSD_LPSURFACE;
5397 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5398 ok(hr == DDERR_INVALIDPARAMS, "Setting width and lpSurface without pitch returned %#x.\n", hr);
5400 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5401 U1(ddsd).lPitch = 16 * 4;
5402 ddsd.dwWidth = 16;
5403 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5404 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5406 reset_ddsd(&ddsd);
5407 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &ddsd);
5408 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5409 ok(ddsd.dwWidth == 16, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
5410 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
5411 ok(U1(ddsd).lPitch == 16 * 4, "SetSurfaceDesc: Expected pitch 64, got %u.\n", U1(ddsd).lPitch);
5413 /* The pitch must be 32 bit aligned and > 0, but is not verified for sanity otherwise.
5415 * VMware rejects those calls, but all real drivers accept it. Mark the VMware behavior broken. */
5416 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5417 U1(ddsd).lPitch = 4 * 4;
5418 ddsd.lpSurface = data;
5419 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5420 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
5422 U1(ddsd).lPitch = 4;
5423 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5424 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
5426 U1(ddsd).lPitch = 16 * 4 + 1;
5427 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5428 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
5430 U1(ddsd).lPitch = 16 * 4 + 3;
5431 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5432 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
5434 U1(ddsd).lPitch = -4;
5435 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5436 ok(hr == DDERR_INVALIDPARAMS, "Setting negative pitch returned %#x.\n", hr);
5438 U1(ddsd).lPitch = 16 * 4;
5439 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5440 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5442 reset_ddsd(&ddsd);
5443 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5444 U1(ddsd).lPitch = 0;
5445 ddsd.dwWidth = 16;
5446 ddsd.lpSurface = data;
5447 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5448 ok(hr == DDERR_INVALIDPARAMS, "Setting zero pitch returned %#x.\n", hr);
5450 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5451 U1(ddsd).lPitch = 16 * 4;
5452 ddsd.dwWidth = 0;
5453 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5454 ok(hr == DDERR_INVALIDPARAMS, "Setting zero width returned %#x.\n", hr);
5456 /* Setting the pixelformat without LPSURFACE is an error, but with LPSURFACE it works. */
5457 ddsd.dwFlags = DDSD_PIXELFORMAT;
5458 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5459 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5460 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5461 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5462 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5463 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5464 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5465 ok(hr == DDERR_INVALIDPARAMS, "Setting the pixel format returned %#x.\n", hr);
5467 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_LPSURFACE;
5468 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5469 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5471 /* Can't set color keys. */
5472 reset_ddsd(&ddsd);
5473 ddsd.dwFlags = DDSD_CKSRCBLT;
5474 ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00ff0000;
5475 ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00ff0000;
5476 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5477 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
5479 ddsd.dwFlags = DDSD_CKSRCBLT | DDSD_LPSURFACE;
5480 ddsd.lpSurface = data;
5481 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5482 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
5484 IDirectDrawSurface7_Release(surface);
5486 /* SetSurfaceDesc needs systemmemory surfaces.
5488 * As a sidenote, fourcc surfaces aren't allowed in sysmem, thus testing DDSD_LINEARSIZE is moot. */
5489 for (i = 0; i < sizeof(invalid_caps_tests) / sizeof(*invalid_caps_tests); i++)
5491 reset_ddsd(&ddsd);
5492 ddsd.dwFlags = DDSD_CAPS;
5493 ddsd.ddsCaps.dwCaps = invalid_caps_tests[i].caps;
5494 ddsd.ddsCaps.dwCaps2 = invalid_caps_tests[i].caps2;
5495 if (!(invalid_caps_tests[i].caps & DDSCAPS_PRIMARYSURFACE))
5497 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5498 ddsd.dwWidth = 8;
5499 ddsd.dwHeight = 8;
5500 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5501 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5502 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5503 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5504 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5505 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5508 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
5509 ok(SUCCEEDED(hr) || hr == DDERR_NODIRECTDRAWHW, "Failed to create surface, hr %#x.\n", hr);
5510 if (FAILED(hr))
5512 skip("Cannot create a %s surface, skipping vidmem SetSurfaceDesc test.\n",
5513 invalid_caps_tests[i].name);
5514 goto done;
5517 reset_ddsd(&ddsd);
5518 ddsd.dwFlags = DDSD_LPSURFACE;
5519 ddsd.lpSurface = data;
5520 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5521 if (invalid_caps_tests[i].supported)
5523 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5525 else
5527 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
5528 invalid_caps_tests[i].name, hr);
5530 /* Check priority of error conditions. */
5531 ddsd.dwFlags = DDSD_WIDTH;
5532 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5533 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
5534 invalid_caps_tests[i].name, hr);
5537 IDirectDrawSurface7_Release(surface);
5540 done:
5541 ref = IDirectDraw7_Release(ddraw);
5542 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5543 DestroyWindow(window);
5546 static void test_user_memory_getdc(void)
5548 IDirectDraw7 *ddraw;
5549 HWND window;
5550 HRESULT hr;
5551 DDSURFACEDESC2 ddsd;
5552 IDirectDrawSurface7 *surface;
5553 DWORD data[16][16];
5554 ULONG ref;
5555 HDC dc;
5556 unsigned int x, y;
5558 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5559 0, 0, 640, 480, 0, 0, 0, 0);
5560 ddraw = create_ddraw();
5561 ok(!!ddraw, "Failed to create a ddraw object.\n");
5562 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5563 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5565 reset_ddsd(&ddsd);
5566 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
5567 ddsd.dwWidth = 16;
5568 ddsd.dwHeight = 16;
5569 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5570 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5571 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5572 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5573 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5574 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5575 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5576 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
5577 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5579 memset(data, 0xaa, sizeof(data));
5580 reset_ddsd(&ddsd);
5581 ddsd.dwFlags = DDSD_LPSURFACE;
5582 ddsd.lpSurface = data;
5583 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5584 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5586 hr = IDirectDrawSurface7_GetDC(surface, &dc);
5587 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5588 BitBlt(dc, 0, 0, 16, 8, NULL, 0, 0, WHITENESS);
5589 BitBlt(dc, 0, 8, 16, 8, NULL, 0, 0, BLACKNESS);
5590 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
5591 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5593 ok(data[0][0] == 0xffffffff, "Expected color 0xffffffff, got %#x.\n", data[0][0]);
5594 ok(data[15][15] == 0x00000000, "Expected color 0x00000000, got %#x.\n", data[15][15]);
5596 ddsd.dwFlags = DDSD_LPSURFACE | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH;
5597 ddsd.lpSurface = data;
5598 ddsd.dwWidth = 4;
5599 ddsd.dwHeight = 8;
5600 U1(ddsd).lPitch = sizeof(*data);
5601 hr = IDirectDrawSurface7_SetSurfaceDesc(surface, &ddsd, 0);
5602 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5604 memset(data, 0xaa, sizeof(data));
5605 hr = IDirectDrawSurface7_GetDC(surface, &dc);
5606 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5607 BitBlt(dc, 0, 0, 4, 8, NULL, 0, 0, BLACKNESS);
5608 BitBlt(dc, 1, 1, 2, 2, NULL, 0, 0, WHITENESS);
5609 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
5610 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5612 for (y = 0; y < 4; y++)
5614 for (x = 0; x < 4; x++)
5616 if ((x == 1 || x == 2) && (y == 1 || y == 2))
5617 ok(data[y][x] == 0xffffffff, "Expected color 0xffffffff on position %ux%u, got %#x.\n",
5618 x, y, data[y][x]);
5619 else
5620 ok(data[y][x] == 0x00000000, "Expected color 0x00000000 on position %ux%u, got %#x.\n",
5621 x, y, data[y][x]);
5624 ok(data[0][5] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 5x0, got %#x.\n",
5625 data[0][5]);
5626 ok(data[7][3] == 0x00000000, "Expected color 0x00000000 on position 3x7, got %#x.\n",
5627 data[7][3]);
5628 ok(data[7][4] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 4x7, got %#x.\n",
5629 data[7][4]);
5630 ok(data[8][0] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 0x8, got %#x.\n",
5631 data[8][0]);
5633 IDirectDrawSurface7_Release(surface);
5634 ref = IDirectDraw7_Release(ddraw);
5635 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5636 DestroyWindow(window);
5639 static void test_sysmem_overlay(void)
5641 IDirectDraw7 *ddraw;
5642 HWND window;
5643 HRESULT hr;
5644 DDSURFACEDESC2 ddsd;
5645 IDirectDrawSurface7 *surface;
5646 ULONG ref;
5648 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5649 0, 0, 640, 480, 0, 0, 0, 0);
5650 ddraw = create_ddraw();
5651 ok(!!ddraw, "Failed to create a ddraw object.\n");
5652 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5653 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5655 reset_ddsd(&ddsd);
5656 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
5657 ddsd.dwWidth = 16;
5658 ddsd.dwHeight = 16;
5659 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
5660 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5661 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5662 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5663 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5664 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5665 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5666 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &surface, NULL);
5667 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
5669 ref = IDirectDraw7_Release(ddraw);
5670 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5671 DestroyWindow(window);
5674 static void test_primary_palette(void)
5676 DDSCAPS2 surface_caps = {DDSCAPS_FLIP, 0, 0, 0};
5677 IDirectDrawSurface7 *primary, *backbuffer;
5678 PALETTEENTRY palette_entries[256];
5679 IDirectDrawPalette *palette, *tmp;
5680 DDSURFACEDESC2 surface_desc;
5681 IDirectDraw7 *ddraw;
5682 DWORD palette_caps;
5683 ULONG refcount;
5684 HWND window;
5685 HRESULT hr;
5687 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5688 0, 0, 640, 480, 0, 0, 0, 0);
5689 ddraw = create_ddraw();
5690 ok(!!ddraw, "Failed to create a ddraw object.\n");
5691 if (FAILED(IDirectDraw7_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
5693 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
5694 IDirectDraw7_Release(ddraw);
5695 DestroyWindow(window);
5696 return;
5698 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5699 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5701 memset(&surface_desc, 0, sizeof(surface_desc));
5702 surface_desc.dwSize = sizeof(surface_desc);
5703 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
5704 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
5705 surface_desc.dwBackBufferCount = 1;
5706 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &primary, NULL);
5707 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5708 hr = IDirectDrawSurface7_GetAttachedSurface(primary, &surface_caps, &backbuffer);
5709 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5711 memset(palette_entries, 0, sizeof(palette_entries));
5712 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
5713 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5714 refcount = get_refcount((IUnknown *)palette);
5715 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5717 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
5718 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
5719 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
5721 hr = IDirectDrawSurface7_SetPalette(primary, palette);
5722 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5724 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
5725 * and is generally somewhat broken with respect to 8 bpp / palette
5726 * handling. */
5727 if (SUCCEEDED(IDirectDrawSurface7_GetPalette(backbuffer, &tmp)))
5729 win_skip("Broken palette handling detected, skipping tests.\n");
5730 IDirectDrawPalette_Release(tmp);
5731 IDirectDrawPalette_Release(palette);
5732 /* The Windows 8 testbot keeps extra references to the primary and
5733 * backbuffer while in 8 bpp mode. */
5734 hr = IDirectDraw7_RestoreDisplayMode(ddraw);
5735 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
5736 goto done;
5739 refcount = get_refcount((IUnknown *)palette);
5740 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5742 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
5743 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
5744 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
5745 "Got unexpected palette caps %#x.\n", palette_caps);
5747 hr = IDirectDrawSurface7_SetPalette(primary, NULL);
5748 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5749 refcount = get_refcount((IUnknown *)palette);
5750 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5752 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
5753 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
5754 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
5756 hr = IDirectDrawSurface7_SetPalette(primary, palette);
5757 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5758 refcount = get_refcount((IUnknown *)palette);
5759 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5761 hr = IDirectDrawSurface7_GetPalette(primary, &tmp);
5762 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
5763 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
5764 IDirectDrawPalette_Release(tmp);
5765 hr = IDirectDrawSurface7_GetPalette(backbuffer, &tmp);
5766 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
5768 refcount = IDirectDrawPalette_Release(palette);
5769 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5770 refcount = IDirectDrawPalette_Release(palette);
5771 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5773 /* Note that this only seems to work when the palette is attached to the
5774 * primary surface. When attached to a regular surface, attempting to get
5775 * the palette here will cause an access violation. */
5776 hr = IDirectDrawSurface7_GetPalette(primary, &tmp);
5777 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
5779 done:
5780 refcount = IDirectDrawSurface7_Release(backbuffer);
5781 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5782 refcount = IDirectDrawSurface7_Release(primary);
5783 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5784 refcount = IDirectDraw7_Release(ddraw);
5785 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5786 DestroyWindow(window);
5789 static HRESULT WINAPI surface_counter(IDirectDrawSurface7 *surface, DDSURFACEDESC2 *desc, void *context)
5791 UINT *surface_count = context;
5793 ++(*surface_count);
5794 IDirectDrawSurface_Release(surface);
5796 return DDENUMRET_OK;
5799 static void test_surface_attachment(void)
5801 IDirectDrawSurface7 *surface1, *surface2, *surface3, *surface4;
5802 IDirectDrawSurface *surface1v1, *surface2v1;
5803 DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, 0};
5804 DDSURFACEDESC2 surface_desc;
5805 IDirectDraw7 *ddraw;
5806 UINT surface_count;
5807 ULONG refcount;
5808 HWND window;
5809 HRESULT hr;
5811 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5812 0, 0, 640, 480, 0, 0, 0, 0);
5813 ddraw = create_ddraw();
5814 ok(!!ddraw, "Failed to create a ddraw object.\n");
5815 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5816 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5818 memset(&surface_desc, 0, sizeof(surface_desc));
5819 surface_desc.dwSize = sizeof(surface_desc);
5820 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
5821 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
5822 U2(surface_desc).dwMipMapCount = 3;
5823 surface_desc.dwWidth = 128;
5824 surface_desc.dwHeight = 128;
5825 if (FAILED(IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface1, NULL)))
5827 skip("Failed to create a texture, skipping tests.\n");
5828 IDirectDraw7_Release(ddraw);
5829 DestroyWindow(window);
5830 return;
5833 hr = IDirectDrawSurface7_GetAttachedSurface(surface1, &caps, &surface2);
5834 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
5835 hr = IDirectDrawSurface7_GetAttachedSurface(surface2, &caps, &surface3);
5836 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
5837 hr = IDirectDrawSurface7_GetAttachedSurface(surface3, &caps, &surface4);
5838 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
5840 surface_count = 0;
5841 IDirectDrawSurface7_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
5842 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
5843 surface_count = 0;
5844 IDirectDrawSurface7_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
5845 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
5846 surface_count = 0;
5847 IDirectDrawSurface7_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
5848 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
5850 memset(&surface_desc, 0, sizeof(surface_desc));
5851 surface_desc.dwSize = sizeof(surface_desc);
5852 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5853 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
5854 surface_desc.dwWidth = 16;
5855 surface_desc.dwHeight = 16;
5856 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
5857 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5859 hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
5860 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5861 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
5862 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5863 hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);
5864 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5865 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
5866 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5867 hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);
5868 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5869 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
5870 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5872 IDirectDrawSurface7_Release(surface4);
5874 memset(&surface_desc, 0, sizeof(surface_desc));
5875 surface_desc.dwSize = sizeof(surface_desc);
5876 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5877 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5878 surface_desc.dwWidth = 16;
5879 surface_desc.dwHeight = 16;
5880 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
5881 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5883 hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
5884 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5885 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
5886 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5887 hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface4);
5888 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5889 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface3);
5890 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5891 hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface4);
5892 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5893 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface2);
5894 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5896 IDirectDrawSurface7_Release(surface4);
5897 IDirectDrawSurface7_Release(surface3);
5898 IDirectDrawSurface7_Release(surface2);
5899 IDirectDrawSurface7_Release(surface1);
5901 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5902 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5904 /* Try a single primary and two offscreen plain surfaces. */
5905 memset(&surface_desc, 0, sizeof(surface_desc));
5906 surface_desc.dwSize = sizeof(surface_desc);
5907 surface_desc.dwFlags = DDSD_CAPS;
5908 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
5909 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
5910 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5912 memset(&surface_desc, 0, sizeof(surface_desc));
5913 surface_desc.dwSize = sizeof(surface_desc);
5914 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5915 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5916 surface_desc.dwWidth = GetSystemMetrics(SM_CXSCREEN);
5917 surface_desc.dwHeight = GetSystemMetrics(SM_CYSCREEN);
5918 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
5919 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5921 memset(&surface_desc, 0, sizeof(surface_desc));
5922 surface_desc.dwSize = sizeof(surface_desc);
5923 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5924 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5925 surface_desc.dwWidth = GetSystemMetrics(SM_CXSCREEN);
5926 surface_desc.dwHeight = GetSystemMetrics(SM_CYSCREEN);
5927 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
5928 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5930 /* This one has a different size. */
5931 memset(&surface_desc, 0, sizeof(surface_desc));
5932 surface_desc.dwSize = sizeof(surface_desc);
5933 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5934 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5935 surface_desc.dwWidth = 128;
5936 surface_desc.dwHeight = 128;
5937 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
5938 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5940 hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
5941 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5942 hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface1);
5943 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5944 hr = IDirectDrawSurface7_AddAttachedSurface(surface2, surface3);
5945 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5946 hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface4);
5947 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5948 hr = IDirectDrawSurface7_AddAttachedSurface(surface4, surface1);
5949 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5951 IDirectDrawSurface7_Release(surface4);
5952 IDirectDrawSurface7_Release(surface3);
5953 IDirectDrawSurface7_Release(surface2);
5954 IDirectDrawSurface7_Release(surface1);
5956 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
5957 memset(&surface_desc, 0, sizeof(surface_desc));
5958 surface_desc.dwSize = sizeof(surface_desc);
5959 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5960 surface_desc.dwWidth = 64;
5961 surface_desc.dwHeight = 64;
5962 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
5963 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
5964 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
5965 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 16;
5966 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0xf800;
5967 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x07e0;
5968 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x001f;
5969 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
5970 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5971 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
5972 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5974 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
5975 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
5976 U1(U4(surface_desc).ddpfPixelFormat).dwZBufferBitDepth = 16;
5977 U3(U4(surface_desc).ddpfPixelFormat).dwZBitMask = 0x0000ffff;
5978 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
5979 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5981 hr = IDirectDrawSurface7_QueryInterface(surface1, &IID_IDirectDrawSurface, (void **)&surface1v1);
5982 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
5983 hr = IDirectDrawSurface7_QueryInterface(surface2, &IID_IDirectDrawSurface, (void **)&surface2v1);
5984 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
5986 hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
5987 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
5988 refcount = get_refcount((IUnknown *)surface2);
5989 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
5990 refcount = get_refcount((IUnknown *)surface2v1);
5991 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
5992 hr = IDirectDrawSurface7_AddAttachedSurface(surface1, surface2);
5993 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
5994 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
5995 todo_wine ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
5996 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
5997 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
5999 /* Attaching while already attached to other surface. */
6000 hr = IDirectDrawSurface7_AddAttachedSurface(surface3, surface2);
6001 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6002 hr = IDirectDrawSurface7_DeleteAttachedSurface(surface3, 0, surface2);
6003 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6004 IDirectDrawSurface7_Release(surface3);
6006 hr = IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface2);
6007 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6008 refcount = get_refcount((IUnknown *)surface2);
6009 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6010 refcount = get_refcount((IUnknown *)surface2v1);
6011 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6013 /* DeleteAttachedSurface() when attaching via IDirectDrawSurface. */
6014 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
6015 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6016 hr = IDirectDrawSurface7_DeleteAttachedSurface(surface1, 0, surface2);
6017 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
6018 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
6019 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6020 refcount = IDirectDrawSurface7_Release(surface2);
6021 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6022 refcount = IDirectDrawSurface7_Release(surface1);
6023 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6025 /* Automatic detachment on release. */
6026 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
6027 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6028 refcount = get_refcount((IUnknown *)surface2v1);
6029 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6030 refcount = IDirectDrawSurface_Release(surface1v1);
6031 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6032 refcount = IDirectDrawSurface_Release(surface2v1);
6033 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6034 refcount = IDirectDraw7_Release(ddraw);
6035 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6036 DestroyWindow(window);
6039 static void test_private_data(void)
6041 IDirectDraw7 *ddraw;
6042 IDirectDrawSurface7 *surface, *surface2;
6043 DDSURFACEDESC2 surface_desc;
6044 ULONG refcount, refcount2, refcount3;
6045 IUnknown *ptr;
6046 DWORD size = sizeof(ptr);
6047 HRESULT hr;
6048 HWND window;
6049 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, 0};
6050 DWORD data[] = {1, 2, 3, 4};
6051 DDCAPS hal_caps;
6052 static const GUID ddraw_private_data_test_guid =
6054 0xfdb37466,
6055 0x428f,
6056 0x4edf,
6057 {0xa3,0x7f,0x9b,0x1d,0xf4,0x88,0xc5,0xfc}
6059 static const GUID ddraw_private_data_test_guid2 =
6061 0x2e5afac2,
6062 0x87b5,
6063 0x4c10,
6064 {0x9b,0x4b,0x89,0xd7,0xd1,0x12,0xe7,0x2b}
6067 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6068 0, 0, 640, 480, 0, 0, 0, 0);
6069 ddraw = create_ddraw();
6070 ok(!!ddraw, "Failed to create a ddraw object.\n");
6071 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6072 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6074 reset_ddsd(&surface_desc);
6075 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
6076 surface_desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
6077 surface_desc.dwHeight = 4;
6078 surface_desc.dwWidth = 4;
6079 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6080 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6082 /* NULL pointers are not valid, but don't cause a crash. */
6083 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL,
6084 sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
6085 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6086 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 0, 0);
6087 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6088 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 1, 0);
6089 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6091 /* DDSPD_IUNKNOWNPOINTER needs sizeof(IUnknown *) bytes of data. */
6092 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6093 0, DDSPD_IUNKNOWNPOINTER);
6094 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6095 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6096 5, DDSPD_IUNKNOWNPOINTER);
6097 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6098 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6099 sizeof(ddraw) * 2, DDSPD_IUNKNOWNPOINTER);
6100 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6102 /* Note that with a size != 0 and size != sizeof(IUnknown *) and
6103 * DDSPD_IUNKNOWNPOINTER set SetPrivateData in ddraw4 and ddraw7
6104 * erases the old content and returns an error. This behavior has
6105 * been fixed in d3d8 and d3d9. Unless an application is found
6106 * that depends on this we don't care about this behavior. */
6107 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6108 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6109 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6110 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6111 0, DDSPD_IUNKNOWNPOINTER);
6112 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6113 size = sizeof(ptr);
6114 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
6115 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
6116 hr = IDirectDrawSurface7_FreePrivateData(surface, &ddraw_private_data_test_guid);
6117 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
6119 refcount = get_refcount((IUnknown *)ddraw);
6120 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6121 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6122 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6123 refcount2 = get_refcount((IUnknown *)ddraw);
6124 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
6126 hr = IDirectDrawSurface7_FreePrivateData(surface, &ddraw_private_data_test_guid);
6127 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
6128 refcount2 = get_refcount((IUnknown *)ddraw);
6129 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
6131 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6132 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6133 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6134 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, surface,
6135 sizeof(surface), DDSPD_IUNKNOWNPOINTER);
6136 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6137 refcount2 = get_refcount((IUnknown *)ddraw);
6138 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
6140 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6141 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6142 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6143 size = 2 * sizeof(ptr);
6144 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
6145 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
6146 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
6147 refcount2 = get_refcount(ptr);
6148 /* Object is NOT addref'ed by the getter. */
6149 ok(ptr == (IUnknown *)ddraw, "Returned interface pointer is %p, expected %p.\n", ptr, ddraw);
6150 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
6152 ptr = (IUnknown *)0xdeadbeef;
6153 size = 1;
6154 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
6155 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
6156 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
6157 size = 2 * sizeof(ptr);
6158 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
6159 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6160 ok(size == 2 * sizeof(ptr), "Got unexpected size %u.\n", size);
6161 size = 1;
6162 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
6163 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
6164 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
6165 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
6166 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid2, NULL, NULL);
6167 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6168 size = 0xdeadbabe;
6169 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid2, &ptr, &size);
6170 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6171 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
6172 ok(size == 0xdeadbabe, "Got unexpected size %u.\n", size);
6173 hr = IDirectDrawSurface7_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, NULL);
6174 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6176 refcount3 = IDirectDrawSurface7_Release(surface);
6177 ok(!refcount3, "Got unexpected refcount %u.\n", refcount3);
6179 /* Destroying the surface frees the reference held on the private data. It also frees
6180 * the reference the surface is holding on its creating object. */
6181 refcount2 = get_refcount((IUnknown *)ddraw);
6182 ok(refcount2 == refcount - 1, "Got unexpected refcount %u.\n", refcount2);
6184 memset(&hal_caps, 0, sizeof(hal_caps));
6185 hal_caps.dwSize = sizeof(hal_caps);
6186 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
6187 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6188 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) == (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6190 reset_ddsd(&surface_desc);
6191 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_MIPMAPCOUNT;
6192 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6193 surface_desc.dwHeight = 4;
6194 surface_desc.dwWidth = 4;
6195 U2(surface_desc).dwMipMapCount = 2;
6196 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6197 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6198 hr = IDirectDrawSurface7_GetAttachedSurface(surface, &caps, &surface2);
6199 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6201 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, data, sizeof(data), 0);
6202 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6203 hr = IDirectDrawSurface7_GetPrivateData(surface2, &ddraw_private_data_test_guid, NULL, NULL);
6204 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6206 IDirectDrawSurface7_Release(surface2);
6207 IDirectDrawSurface7_Release(surface);
6209 else
6210 skip("Mipmapped textures not supported, skipping mipmap private data test.\n");
6212 refcount = IDirectDraw7_Release(ddraw);
6213 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6214 DestroyWindow(window);
6217 static void test_pixel_format(void)
6219 HWND window, window2 = NULL;
6220 HDC hdc, hdc2 = NULL;
6221 HMODULE gl = NULL;
6222 int format, test_format;
6223 PIXELFORMATDESCRIPTOR pfd;
6224 IDirectDraw7 *ddraw = NULL;
6225 IDirectDrawClipper *clipper = NULL;
6226 DDSURFACEDESC2 ddsd;
6227 IDirectDrawSurface7 *primary = NULL;
6228 DDBLTFX fx;
6229 HRESULT hr;
6231 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
6232 100, 100, 160, 160, NULL, NULL, NULL, NULL);
6233 if (!window)
6235 skip("Failed to create window\n");
6236 return;
6239 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
6240 100, 100, 160, 160, NULL, NULL, NULL, NULL);
6242 hdc = GetDC(window);
6243 if (!hdc)
6245 skip("Failed to get DC\n");
6246 goto cleanup;
6249 if (window2)
6250 hdc2 = GetDC(window2);
6252 gl = LoadLibraryA("opengl32.dll");
6253 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
6255 format = GetPixelFormat(hdc);
6256 ok(format == 0, "new window has pixel format %d\n", format);
6258 ZeroMemory(&pfd, sizeof(pfd));
6259 pfd.nSize = sizeof(pfd);
6260 pfd.nVersion = 1;
6261 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
6262 pfd.iPixelType = PFD_TYPE_RGBA;
6263 pfd.iLayerType = PFD_MAIN_PLANE;
6264 format = ChoosePixelFormat(hdc, &pfd);
6265 if (format <= 0)
6267 skip("no pixel format available\n");
6268 goto cleanup;
6271 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
6273 skip("failed to set pixel format\n");
6274 goto cleanup;
6277 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
6279 skip("failed to set pixel format on second window\n");
6280 if (hdc2)
6282 ReleaseDC(window2, hdc2);
6283 hdc2 = NULL;
6287 ddraw = create_ddraw();
6288 ok(!!ddraw, "Failed to create a ddraw object.\n");
6290 test_format = GetPixelFormat(hdc);
6291 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6293 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6294 if (FAILED(hr))
6296 skip("Failed to set cooperative level, hr %#x.\n", hr);
6297 goto cleanup;
6300 test_format = GetPixelFormat(hdc);
6301 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6303 if (hdc2)
6305 hr = IDirectDraw7_CreateClipper(ddraw, 0, &clipper, NULL);
6306 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
6307 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
6308 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
6310 test_format = GetPixelFormat(hdc);
6311 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6313 test_format = GetPixelFormat(hdc2);
6314 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6317 memset(&ddsd, 0, sizeof(ddsd));
6318 ddsd.dwSize = sizeof(ddsd);
6319 ddsd.dwFlags = DDSD_CAPS;
6320 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6322 hr = IDirectDraw7_CreateSurface(ddraw, &ddsd, &primary, NULL);
6323 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
6325 test_format = GetPixelFormat(hdc);
6326 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6328 if (hdc2)
6330 test_format = GetPixelFormat(hdc2);
6331 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6334 if (clipper)
6336 hr = IDirectDrawSurface7_SetClipper(primary, clipper);
6337 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
6339 test_format = GetPixelFormat(hdc);
6340 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6342 test_format = GetPixelFormat(hdc2);
6343 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6346 memset(&fx, 0, sizeof(fx));
6347 fx.dwSize = sizeof(fx);
6348 hr = IDirectDrawSurface7_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6349 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
6351 test_format = GetPixelFormat(hdc);
6352 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6354 if (hdc2)
6356 test_format = GetPixelFormat(hdc2);
6357 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6360 cleanup:
6361 if (primary) IDirectDrawSurface7_Release(primary);
6362 if (clipper) IDirectDrawClipper_Release(clipper);
6363 if (ddraw) IDirectDraw7_Release(ddraw);
6364 if (gl) FreeLibrary(gl);
6365 if (hdc) ReleaseDC(window, hdc);
6366 if (hdc2) ReleaseDC(window2, hdc2);
6367 if (window) DestroyWindow(window);
6368 if (window2) DestroyWindow(window2);
6371 static void test_create_surface_pitch(void)
6373 IDirectDrawSurface7 *surface;
6374 DDSURFACEDESC2 surface_desc;
6375 IDirectDraw7 *ddraw;
6376 unsigned int i;
6377 ULONG refcount;
6378 HWND window;
6379 HRESULT hr;
6380 void *mem;
6382 static const struct
6384 DWORD placement;
6385 DWORD flags_in;
6386 DWORD pitch_in;
6387 HRESULT hr;
6388 DWORD flags_out;
6389 DWORD pitch_out32;
6390 DWORD pitch_out64;
6392 test_data[] =
6394 {DDSCAPS_VIDEOMEMORY, 0, 0, DD_OK,
6395 DDSD_PITCH, 0x100, 0x100},
6396 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x104, DD_OK,
6397 DDSD_PITCH, 0x100, 0x100},
6398 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
6399 DDSD_PITCH, 0x100, 0x100},
6400 {DDSCAPS_VIDEOMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
6401 0, 0, 0 },
6402 {DDSCAPS_SYSTEMMEMORY, 0, 0, DD_OK,
6403 DDSD_PITCH, 0x100, 0x0fc},
6404 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x104, DD_OK,
6405 DDSD_PITCH, 0x100, 0x0fc},
6406 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
6407 DDSD_PITCH, 0x100, 0x0fc},
6408 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
6409 DDSD_PITCH, 0x100, 0x0fc},
6410 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
6411 0, 0, 0 },
6412 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DD_OK,
6413 DDSD_PITCH, 0x100, 0x100},
6414 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0fe, DDERR_INVALIDPARAMS,
6415 0, 0, 0 },
6416 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0fc, DD_OK,
6417 DDSD_PITCH, 0x0fc, 0x0fc},
6418 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0f8, DDERR_INVALIDPARAMS,
6419 0, 0, 0 },
6420 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x100, DDERR_INVALIDPARAMS,
6421 0, 0, 0 },
6422 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x3f00, DDERR_INVALIDPARAMS,
6423 0, 0, 0 },
6424 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, 0x100, DD_OK,
6425 DDSD_PITCH, 0x100, 0x100},
6427 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
6429 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6430 0, 0, 640, 480, 0, 0, 0, 0);
6431 ddraw = create_ddraw();
6432 ok(!!ddraw, "Failed to create a ddraw object.\n");
6433 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6434 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6436 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
6438 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
6440 memset(&surface_desc, 0, sizeof(surface_desc));
6441 surface_desc.dwSize = sizeof(surface_desc);
6442 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
6443 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | test_data[i].placement;
6444 surface_desc.dwWidth = 63;
6445 surface_desc.dwHeight = 63;
6446 U1(surface_desc).lPitch = test_data[i].pitch_in;
6447 surface_desc.lpSurface = mem;
6448 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6449 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
6450 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
6451 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6452 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6453 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6454 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6455 ok(hr == test_data[i].hr || (test_data[i].placement == DDSCAPS_VIDEOMEMORY && hr == DDERR_NODIRECTDRAWHW),
6456 "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
6457 if (FAILED(hr))
6458 continue;
6460 memset(&surface_desc, 0, sizeof(surface_desc));
6461 surface_desc.dwSize = sizeof(surface_desc);
6462 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &surface_desc);
6463 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
6464 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
6465 "Test %u: Got unexpected flags %#x, expected %#x.\n",
6466 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
6467 if (sizeof(void *) != sizeof(DWORD) && test_data[i].pitch_out32 != test_data[i].pitch_out64)
6468 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
6469 "Test %u: Got unexpected pitch %u, expected %u.\n",
6470 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
6471 else
6472 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
6473 "Test %u: Got unexpected pitch %u, expected %u.\n",
6474 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
6475 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
6477 IDirectDrawSurface7_Release(surface);
6480 HeapFree(GetProcessHeap(), 0, mem);
6481 refcount = IDirectDraw7_Release(ddraw);
6482 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6483 DestroyWindow(window);
6486 static void test_mipmap_lock(void)
6488 IDirectDrawSurface7 *surface, *surface2;
6489 DDSURFACEDESC2 surface_desc;
6490 IDirectDraw7 *ddraw;
6491 ULONG refcount;
6492 HWND window;
6493 HRESULT hr;
6494 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, 0};
6495 DDCAPS hal_caps;
6497 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6498 0, 0, 640, 480, 0, 0, 0, 0);
6499 ddraw = create_ddraw();
6500 ok(!!ddraw, "Failed to create a ddraw object.\n");
6501 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6502 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6504 memset(&hal_caps, 0, sizeof(hal_caps));
6505 hal_caps.dwSize = sizeof(hal_caps);
6506 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
6507 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6508 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6510 skip("Mipmapped textures not supported, skipping mipmap lock test.\n");
6511 IDirectDraw7_Release(ddraw);
6512 DestroyWindow(window);
6513 return;
6516 memset(&surface_desc, 0, sizeof(surface_desc));
6517 surface_desc.dwSize = sizeof(surface_desc);
6518 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
6519 surface_desc.dwWidth = 4;
6520 surface_desc.dwHeight = 4;
6521 U2(surface_desc).dwMipMapCount = 2;
6522 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
6523 | DDSCAPS_SYSTEMMEMORY;
6524 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6525 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6526 hr = IDirectDrawSurface7_GetAttachedSurface(surface, &caps, &surface2);
6527 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6529 memset(&surface_desc, 0, sizeof(surface_desc));
6530 surface_desc.dwSize = sizeof(surface_desc);
6531 hr = IDirectDrawSurface7_Lock(surface, NULL, &surface_desc, 0, NULL);
6532 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6533 memset(&surface_desc, 0, sizeof(surface_desc));
6534 surface_desc.dwSize = sizeof(surface_desc);
6535 hr = IDirectDrawSurface7_Lock(surface2, NULL, &surface_desc, 0, NULL);
6536 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6537 IDirectDrawSurface7_Unlock(surface2, NULL);
6538 IDirectDrawSurface7_Unlock(surface, NULL);
6540 IDirectDrawSurface7_Release(surface2);
6541 IDirectDrawSurface7_Release(surface);
6542 refcount = IDirectDraw7_Release(ddraw);
6543 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6544 DestroyWindow(window);
6547 static void test_palette_complex(void)
6549 IDirectDrawSurface7 *surface, *mipmap, *tmp;
6550 DDSURFACEDESC2 surface_desc;
6551 IDirectDraw7 *ddraw;
6552 IDirectDrawPalette *palette, *palette2;
6553 ULONG refcount;
6554 HWND window;
6555 HRESULT hr;
6556 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, 0};
6557 DDCAPS hal_caps;
6558 PALETTEENTRY palette_entries[256];
6559 unsigned int i;
6561 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6562 0, 0, 640, 480, 0, 0, 0, 0);
6563 ddraw = create_ddraw();
6564 ok(!!ddraw, "Failed to create a ddraw object.\n");
6565 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6566 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6568 memset(&hal_caps, 0, sizeof(hal_caps));
6569 hal_caps.dwSize = sizeof(hal_caps);
6570 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
6571 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6572 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6574 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
6575 IDirectDraw7_Release(ddraw);
6576 DestroyWindow(window);
6577 return;
6580 memset(&surface_desc, 0, sizeof(surface_desc));
6581 surface_desc.dwSize = sizeof(surface_desc);
6582 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6583 surface_desc.dwWidth = 128;
6584 surface_desc.dwHeight = 128;
6585 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6586 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6587 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
6588 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
6589 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6590 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6592 memset(palette_entries, 0, sizeof(palette_entries));
6593 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6594 palette_entries, &palette, NULL);
6595 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6597 palette2 = (void *)0xdeadbeef;
6598 hr = IDirectDrawSurface7_GetPalette(surface, &palette2);
6599 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6600 ok(!palette2, "Got unexpected palette %p.\n", palette2);
6601 hr = IDirectDrawSurface7_SetPalette(surface, palette);
6602 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6603 hr = IDirectDrawSurface7_GetPalette(surface, &palette2);
6604 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
6605 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
6606 IDirectDrawPalette_Release(palette2);
6608 mipmap = surface;
6609 IDirectDrawSurface7_AddRef(mipmap);
6610 for (i = 0; i < 7; ++i)
6612 hr = IDirectDrawSurface7_GetAttachedSurface(mipmap, &caps, &tmp);
6613 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
6614 palette2 = (void *)0xdeadbeef;
6615 hr = IDirectDrawSurface7_GetPalette(tmp, &palette2);
6616 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
6617 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
6619 hr = IDirectDrawSurface7_SetPalette(tmp, palette);
6620 ok(hr == DDERR_NOTONMIPMAPSUBLEVEL, "Got unexpected hr %#x, i %u.\n", hr, i);
6622 hr = IDirectDrawSurface7_GetPalette(tmp, &palette2);
6623 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
6624 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
6626 /* Ddraw7 uses the palette of the mipmap for GetDC, just like previous
6627 * ddraw versions. Combined with the test results above this means no
6628 * palette is available. So depending on the driver either GetDC fails
6629 * or the DIB color table contains random data. */
6631 IDirectDrawSurface7_Release(mipmap);
6632 mipmap = tmp;
6635 hr = IDirectDrawSurface7_GetAttachedSurface(mipmap, &caps, &tmp);
6636 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6637 IDirectDrawSurface7_Release(mipmap);
6638 refcount = IDirectDrawSurface7_Release(surface);
6639 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6641 /* Test DDERR_INVALIDPIXELFORMAT vs DDERR_NOTONMIPMAPSUBLEVEL. */
6642 memset(&surface_desc, 0, sizeof(surface_desc));
6643 surface_desc.dwSize = sizeof(surface_desc);
6644 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6645 surface_desc.dwWidth = 128;
6646 surface_desc.dwHeight = 128;
6647 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6648 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6649 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
6650 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
6651 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6652 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6653 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6654 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6655 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6657 hr = IDirectDrawSurface7_GetAttachedSurface(surface, &caps, &mipmap);
6658 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6659 hr = IDirectDrawSurface7_SetPalette(mipmap, palette);
6660 ok(hr == DDERR_NOTONMIPMAPSUBLEVEL, "Got unexpected hr %#x.\n", hr);
6662 IDirectDrawSurface7_Release(mipmap);
6663 refcount = IDirectDrawSurface7_Release(surface);
6664 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6665 refcount = IDirectDrawPalette_Release(palette);
6666 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6668 refcount = IDirectDraw7_Release(ddraw);
6669 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6670 DestroyWindow(window);
6673 static void test_p8_rgb_blit(void)
6675 IDirectDrawSurface7 *src, *dst;
6676 DDSURFACEDESC2 surface_desc;
6677 IDirectDraw7 *ddraw;
6678 IDirectDrawPalette *palette;
6679 ULONG refcount;
6680 HWND window;
6681 HRESULT hr;
6682 PALETTEENTRY palette_entries[256];
6683 unsigned int x;
6684 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
6685 static const D3DCOLOR expected[] =
6687 0x00101010, 0x00010101, 0x00020202, 0x00030303,
6688 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
6690 D3DCOLOR color;
6692 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6693 0, 0, 640, 480, 0, 0, 0, 0);
6694 ddraw = create_ddraw();
6695 ok(!!ddraw, "Failed to create a ddraw object.\n");
6696 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6697 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6699 memset(palette_entries, 0, sizeof(palette_entries));
6700 palette_entries[1].peGreen = 0xff;
6701 palette_entries[2].peBlue = 0xff;
6702 palette_entries[3].peFlags = 0xff;
6703 palette_entries[4].peRed = 0xff;
6704 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6705 palette_entries, &palette, NULL);
6706 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6708 memset(&surface_desc, 0, sizeof(surface_desc));
6709 surface_desc.dwSize = sizeof(surface_desc);
6710 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6711 surface_desc.dwWidth = 8;
6712 surface_desc.dwHeight = 1;
6713 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6714 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6715 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
6716 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
6717 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &src, NULL);
6718 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6720 memset(&surface_desc, 0, sizeof(surface_desc));
6721 surface_desc.dwSize = sizeof(surface_desc);
6722 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6723 surface_desc.dwWidth = 8;
6724 surface_desc.dwHeight = 1;
6725 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6726 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6727 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
6728 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
6729 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6730 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6731 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6732 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
6733 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &dst, NULL);
6734 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6736 memset(&surface_desc, 0, sizeof(surface_desc));
6737 surface_desc.dwSize = sizeof(surface_desc);
6738 hr = IDirectDrawSurface7_Lock(src, NULL, &surface_desc, 0, NULL);
6739 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
6740 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
6741 hr = IDirectDrawSurface7_Unlock(src, NULL);
6742 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
6744 hr = IDirectDrawSurface7_SetPalette(src, palette);
6745 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6746 hr = IDirectDrawSurface7_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
6747 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
6748 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
6749 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
6750 "Failed to blit, hr %#x.\n", hr);
6752 if (SUCCEEDED(hr))
6754 for (x = 0; x < sizeof(expected) / sizeof(*expected); x++)
6756 color = get_surface_color(dst, x, 0);
6757 todo_wine ok(compare_color(color, expected[x], 0),
6758 "Pixel %u: Got color %#x, expected %#x.\n",
6759 x, color, expected[x]);
6763 IDirectDrawSurface7_Release(src);
6764 IDirectDrawSurface7_Release(dst);
6765 IDirectDrawPalette_Release(palette);
6767 refcount = IDirectDraw7_Release(ddraw);
6768 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6769 DestroyWindow(window);
6772 static void test_material(void)
6774 static const D3DCOLORVALUE null_color;
6775 IDirect3DDevice7 *device;
6776 D3DMATERIAL7 material;
6777 ULONG refcount;
6778 HWND window;
6779 HRESULT hr;
6781 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6782 0, 0, 640, 480, 0, 0, 0, 0);
6783 if (!(device = create_device(window, DDSCL_NORMAL)))
6785 skip("Failed to create a 3D device, skipping test.\n");
6786 DestroyWindow(window);
6787 return;
6790 hr = IDirect3DDevice7_GetMaterial(device, &material);
6791 ok(SUCCEEDED(hr), "Failed to get material, hr %#x.\n", hr);
6792 ok(!memcmp(&U(material).diffuse, &null_color, sizeof(null_color)),
6793 "Got unexpected diffuse color {%.8e, %.8e, %.8e, %.8e}.\n",
6794 U1(U(material).diffuse).r, U2(U(material).diffuse).g,
6795 U3(U(material).diffuse).b, U4(U(material).diffuse).a);
6796 ok(!memcmp(&U1(material).ambient, &null_color, sizeof(null_color)),
6797 "Got unexpected ambient color {%.8e, %.8e, %.8e, %.8e}.\n",
6798 U1(U1(material).ambient).r, U2(U1(material).ambient).g,
6799 U3(U1(material).ambient).b, U4(U1(material).ambient).a);
6800 ok(!memcmp(&U2(material).specular, &null_color, sizeof(null_color)),
6801 "Got unexpected specular color {%.8e, %.8e, %.8e, %.8e}.\n",
6802 U1(U2(material).specular).r, U2(U2(material).specular).g,
6803 U3(U2(material).specular).b, U4(U2(material).specular).a);
6804 ok(!memcmp(&U3(material).emissive, &null_color, sizeof(null_color)),
6805 "Got unexpected emissive color {%.8e, %.8e, %.8e, %.8e}.\n",
6806 U1(U3(material).emissive).r, U2(U3(material).emissive).g,
6807 U3(U3(material).emissive).b, U4(U3(material).emissive).a);
6808 ok(U4(material).power == 0.0f, "Got unexpected power %.8e.\n", U4(material).power);
6810 refcount = IDirect3DDevice7_Release(device);
6811 ok(!refcount, "Device has %u references left.\n", refcount);
6812 DestroyWindow(window);
6815 static void test_palette_gdi(void)
6817 IDirectDrawSurface7 *surface, *primary;
6818 DDSURFACEDESC2 surface_desc;
6819 IDirectDraw7 *ddraw;
6820 IDirectDrawPalette *palette, *palette2;
6821 ULONG refcount;
6822 HWND window;
6823 HRESULT hr;
6824 PALETTEENTRY palette_entries[256];
6825 UINT i;
6826 HDC dc;
6827 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
6828 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
6829 * not the point of this test. */
6830 static const RGBQUAD expected1[] =
6832 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
6833 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
6835 static const RGBQUAD expected2[] =
6837 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
6838 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
6840 static const RGBQUAD expected3[] =
6842 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
6843 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
6845 HPALETTE ddraw_palette_handle;
6846 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
6847 RGBQUAD rgbquad[255];
6848 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
6850 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6851 0, 0, 640, 480, 0, 0, 0, 0);
6852 ddraw = create_ddraw();
6853 ok(!!ddraw, "Failed to create a ddraw object.\n");
6854 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6855 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6857 memset(&surface_desc, 0, sizeof(surface_desc));
6858 surface_desc.dwSize = sizeof(surface_desc);
6859 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6860 surface_desc.dwWidth = 16;
6861 surface_desc.dwHeight = 16;
6862 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6863 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6864 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
6865 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
6866 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6867 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6869 /* Avoid colors from the Windows default palette. */
6870 memset(palette_entries, 0, sizeof(palette_entries));
6871 palette_entries[1].peRed = 0x01;
6872 palette_entries[2].peGreen = 0x02;
6873 palette_entries[3].peBlue = 0x03;
6874 palette_entries[4].peRed = 0x13;
6875 palette_entries[4].peGreen = 0x14;
6876 palette_entries[4].peBlue = 0x15;
6877 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6878 palette_entries, &palette, NULL);
6879 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6881 /* If there is no palette assigned and the display mode is not 8 bpp, some
6882 * drivers refuse to create a DC while others allow it. If a DC is created,
6883 * the DIB color table is uninitialized and contains random colors. No error
6884 * is generated when trying to read pixels and random garbage is returned.
6886 * The most likely explanation is that if the driver creates a DC, it (or
6887 * the higher-level runtime) uses GetSystemPaletteEntries to find the
6888 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
6889 * contains uninitialized garbage. See comments below for the P8 case. */
6891 hr = IDirectDrawSurface7_SetPalette(surface, palette);
6892 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6893 hr = IDirectDrawSurface7_GetDC(surface, &dc);
6894 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6895 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
6896 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
6897 "Got unexpected palette %p, expected %p.\n",
6898 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
6900 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
6901 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
6902 for (i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
6904 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
6905 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6906 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
6907 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
6909 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
6911 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
6912 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
6913 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
6916 /* Update the palette while the DC is in use. This does not modify the DC. */
6917 palette_entries[4].peRed = 0x23;
6918 palette_entries[4].peGreen = 0x24;
6919 palette_entries[4].peBlue = 0x25;
6920 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
6921 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
6923 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
6924 ok(i == 1, "Expected count 1, got %u.\n", i);
6925 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
6926 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6927 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
6928 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
6930 /* Neither does re-setting the palette. */
6931 hr = IDirectDrawSurface7_SetPalette(surface, NULL);
6932 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6933 hr = IDirectDrawSurface7_SetPalette(surface, palette);
6934 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6936 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
6937 ok(i == 1, "Expected count 1, got %u.\n", i);
6938 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
6939 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6940 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
6941 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
6943 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
6944 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6946 /* Refresh the DC. This updates the palette. */
6947 hr = IDirectDrawSurface7_GetDC(surface, &dc);
6948 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6949 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
6950 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
6951 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
6953 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
6954 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6955 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
6956 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
6958 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
6960 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
6961 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
6962 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
6964 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
6965 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6967 refcount = IDirectDrawSurface7_Release(surface);
6968 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6970 if (FAILED(IDirectDraw7_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
6972 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
6973 IDirectDrawPalette_Release(palette);
6974 IDirectDraw7_Release(ddraw);
6975 DestroyWindow(window);
6976 return;
6978 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
6979 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
6980 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6982 memset(&surface_desc, 0, sizeof(surface_desc));
6983 surface_desc.dwSize = sizeof(surface_desc);
6984 surface_desc.dwFlags = DDSD_CAPS;
6985 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6986 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &primary, NULL);
6987 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6989 hr = IDirectDrawSurface7_SetPalette(primary, palette);
6990 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6991 hr = IDirectDrawSurface7_GetDC(primary, &dc);
6992 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6993 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
6994 /* Windows 2000 on the testbot assigns a different palette to the primary. Refrast? */
6995 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE) || broken(TRUE),
6996 "Got unexpected palette %p, expected %p.\n",
6997 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
6998 SelectPalette(dc, ddraw_palette_handle, FALSE);
7000 /* The primary uses the system palette. In exclusive mode, the system palette matches
7001 * the ddraw palette attached to the primary, so the result is what you would expect
7002 * from a regular surface. Tests for the interaction between the ddraw palette and
7003 * the system palette are not included pending an application that depends on this.
7004 * The relation between those causes problems on Windows Vista and newer for games
7005 * like Age of Empires or StarcCaft. Don't emulate it without a real need. */
7006 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7007 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7008 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
7010 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7011 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7012 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7013 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7015 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7017 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7018 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7019 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7021 hr = IDirectDrawSurface7_ReleaseDC(primary, dc);
7022 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7024 memset(&surface_desc, 0, sizeof(surface_desc));
7025 surface_desc.dwSize = sizeof(surface_desc);
7026 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7027 surface_desc.dwWidth = 16;
7028 surface_desc.dwHeight = 16;
7029 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7030 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7031 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7033 /* Here the offscreen surface appears to use the primary's palette,
7034 * but in all likelihood it is actually the system palette. */
7035 hr = IDirectDrawSurface7_GetDC(surface, &dc);
7036 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7037 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7038 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7039 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
7041 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7042 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7043 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7044 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7046 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7048 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7049 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7050 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7052 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
7053 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7055 /* On real hardware a change to the primary surface's palette applies immediately,
7056 * even on device contexts from offscreen surfaces that do not have their own
7057 * palette. On the testbot VMs this is not the case. Don't test this until we
7058 * know of an application that depends on this. */
7060 memset(palette_entries, 0, sizeof(palette_entries));
7061 palette_entries[1].peBlue = 0x40;
7062 palette_entries[2].peRed = 0x40;
7063 palette_entries[3].peGreen = 0x40;
7064 palette_entries[4].peRed = 0x12;
7065 palette_entries[4].peGreen = 0x34;
7066 palette_entries[4].peBlue = 0x56;
7067 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7068 palette_entries, &palette2, NULL);
7069 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7070 hr = IDirectDrawSurface7_SetPalette(surface, palette2);
7071 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7073 /* A palette assigned to the offscreen surface overrides the primary / system
7074 * palette. */
7075 hr = IDirectDrawSurface7_GetDC(surface, &dc);
7076 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7077 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7078 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7079 for (i = 0; i < sizeof(expected3) / sizeof(*expected3); i++)
7081 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
7082 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7083 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7084 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
7086 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7088 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7089 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7090 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7092 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
7093 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7095 refcount = IDirectDrawSurface7_Release(surface);
7096 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7098 /* The Windows 8 testbot keeps extra references to the primary and
7099 * backbuffer while in 8 bpp mode. */
7100 hr = IDirectDraw7_RestoreDisplayMode(ddraw);
7101 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
7103 refcount = IDirectDrawSurface7_Release(primary);
7104 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7105 refcount = IDirectDrawPalette_Release(palette2);
7106 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7107 refcount = IDirectDrawPalette_Release(palette);
7108 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7109 refcount = IDirectDraw7_Release(ddraw);
7110 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7111 DestroyWindow(window);
7114 static void test_palette_alpha(void)
7116 IDirectDrawSurface7 *surface;
7117 DDSURFACEDESC2 surface_desc;
7118 IDirectDraw7 *ddraw;
7119 IDirectDrawPalette *palette;
7120 ULONG refcount;
7121 HWND window;
7122 HRESULT hr;
7123 PALETTEENTRY palette_entries[256];
7124 unsigned int i;
7125 static const struct
7127 DWORD caps, flags;
7128 BOOL attach_allowed;
7129 const char *name;
7131 test_data[] =
7133 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
7134 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
7135 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
7138 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7139 0, 0, 640, 480, 0, 0, 0, 0);
7140 ddraw = create_ddraw();
7141 ok(!!ddraw, "Failed to create a ddraw object.\n");
7142 if (FAILED(IDirectDraw7_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
7144 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
7145 IDirectDraw7_Release(ddraw);
7146 DestroyWindow(window);
7147 return;
7149 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7150 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7152 memset(palette_entries, 0, sizeof(palette_entries));
7153 palette_entries[1].peFlags = 0x42;
7154 palette_entries[2].peFlags = 0xff;
7155 palette_entries[3].peFlags = 0x80;
7156 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
7157 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7159 memset(palette_entries, 0x66, sizeof(palette_entries));
7160 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
7161 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
7162 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7163 palette_entries[0].peFlags);
7164 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7165 palette_entries[1].peFlags);
7166 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
7167 palette_entries[2].peFlags);
7168 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
7169 palette_entries[3].peFlags);
7171 IDirectDrawPalette_Release(palette);
7173 memset(palette_entries, 0, sizeof(palette_entries));
7174 palette_entries[1].peFlags = 0x42;
7175 palette_entries[1].peRed = 0xff;
7176 palette_entries[2].peFlags = 0xff;
7177 palette_entries[3].peFlags = 0x80;
7178 hr = IDirectDraw7_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
7179 palette_entries, &palette, NULL);
7180 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7182 memset(palette_entries, 0x66, sizeof(palette_entries));
7183 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
7184 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
7185 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7186 palette_entries[0].peFlags);
7187 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7188 palette_entries[1].peFlags);
7189 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
7190 palette_entries[2].peFlags);
7191 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
7192 palette_entries[3].peFlags);
7194 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
7196 memset(&surface_desc, 0, sizeof(surface_desc));
7197 surface_desc.dwSize = sizeof(surface_desc);
7198 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
7199 surface_desc.dwWidth = 128;
7200 surface_desc.dwHeight = 128;
7201 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
7202 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7203 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
7205 hr = IDirectDrawSurface7_SetPalette(surface, palette);
7206 if (test_data[i].attach_allowed)
7207 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
7208 else
7209 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
7211 if (SUCCEEDED(hr))
7213 HDC dc;
7214 RGBQUAD rgbquad;
7215 UINT retval;
7217 hr = IDirectDrawSurface7_GetDC(surface, &dc);
7218 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
7219 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
7220 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
7221 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
7222 rgbquad.rgbRed, test_data[i].name);
7223 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
7224 rgbquad.rgbGreen, test_data[i].name);
7225 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
7226 rgbquad.rgbBlue, test_data[i].name);
7227 todo_wine ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
7228 rgbquad.rgbReserved, test_data[i].name);
7229 hr = IDirectDrawSurface7_ReleaseDC(surface, dc);
7230 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7232 IDirectDrawSurface7_Release(surface);
7235 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
7236 memset(&surface_desc, 0, sizeof(surface_desc));
7237 surface_desc.dwSize = sizeof(surface_desc);
7238 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7239 surface_desc.dwWidth = 128;
7240 surface_desc.dwHeight = 128;
7241 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7242 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7243 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
7244 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7245 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7246 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7247 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7248 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7249 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7250 hr = IDirectDrawSurface7_SetPalette(surface, palette);
7251 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
7252 IDirectDrawSurface7_Release(surface);
7254 /* The Windows 8 testbot keeps extra references to the primary
7255 * while in 8 bpp mode. */
7256 hr = IDirectDraw7_RestoreDisplayMode(ddraw);
7257 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
7259 refcount = IDirectDrawPalette_Release(palette);
7260 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7261 refcount = IDirectDraw7_Release(ddraw);
7262 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7263 DestroyWindow(window);
7266 static void test_vb_writeonly(void)
7268 IDirect3DDevice7 *device;
7269 IDirect3D7 *d3d;
7270 IDirect3DVertexBuffer7 *buffer;
7271 HWND window;
7272 HRESULT hr;
7273 D3DVERTEXBUFFERDESC desc;
7274 void *ptr;
7275 static const struct vec4 quad[] =
7277 { 0.0f, 480.0f, 0.0f, 1.0f},
7278 { 0.0f, 0.0f, 0.0f, 1.0f},
7279 {640.0f, 480.0f, 0.0f, 1.0f},
7280 {640.0f, 0.0f, 0.0f, 1.0f},
7283 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7284 0, 0, 640, 480, 0, 0, 0, 0);
7286 if (!(device = create_device(window, DDSCL_NORMAL)))
7288 skip("Failed to create a 3D device, skipping test.\n");
7289 DestroyWindow(window);
7290 return;
7293 hr = IDirect3DDevice7_GetDirect3D(device, &d3d);
7294 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
7296 memset(&desc, 0, sizeof(desc));
7297 desc.dwSize = sizeof(desc);
7298 desc.dwCaps = D3DVBCAPS_WRITEONLY;
7299 desc.dwFVF = D3DFVF_XYZRHW;
7300 desc.dwNumVertices = sizeof(quad) / sizeof(*quad);
7301 hr = IDirect3D7_CreateVertexBuffer(d3d, &desc, &buffer, 0);
7302 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
7304 hr = IDirect3DVertexBuffer7_Lock(buffer, DDLOCK_DISCARDCONTENTS, &ptr, NULL);
7305 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
7306 memcpy(ptr, quad, sizeof(quad));
7307 hr = IDirect3DVertexBuffer7_Unlock(buffer);
7308 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
7310 hr = IDirect3DDevice7_BeginScene(device);
7311 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7312 hr = IDirect3DDevice7_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
7313 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7314 hr = IDirect3DDevice7_EndScene(device);
7315 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7317 hr = IDirect3DVertexBuffer7_Lock(buffer, 0, &ptr, NULL);
7318 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
7319 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
7320 hr = IDirect3DVertexBuffer7_Unlock(buffer);
7321 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
7323 hr = IDirect3DVertexBuffer7_Lock(buffer, DDLOCK_READONLY, &ptr, NULL);
7324 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
7325 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
7326 hr = IDirect3DVertexBuffer7_Unlock(buffer);
7327 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
7329 IDirect3DVertexBuffer7_Release(buffer);
7330 IDirect3D7_Release(d3d);
7331 IDirect3DDevice7_Release(device);
7332 DestroyWindow(window);
7335 static void test_lost_device(void)
7337 IDirectDrawSurface7 *surface;
7338 DDSURFACEDESC2 surface_desc;
7339 IDirectDraw7 *ddraw;
7340 ULONG refcount;
7341 HWND window;
7342 HRESULT hr;
7343 BOOL ret;
7345 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7346 0, 0, 640, 480, 0, 0, 0, 0);
7347 ddraw = create_ddraw();
7348 ok(!!ddraw, "Failed to create a ddraw object.\n");
7349 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7350 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7352 memset(&surface_desc, 0, sizeof(surface_desc));
7353 surface_desc.dwSize = sizeof(surface_desc);
7354 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
7355 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
7356 surface_desc.dwBackBufferCount = 1;
7357 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7358 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7360 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7361 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7362 hr = IDirectDrawSurface7_IsLost(surface);
7363 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7364 hr = IDirectDrawSurface7_Flip(surface, NULL, DDFLIP_WAIT);
7365 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7367 ret = SetForegroundWindow(GetDesktopWindow());
7368 ok(ret, "Failed to set foreground window.\n");
7369 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7370 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
7371 hr = IDirectDrawSurface7_IsLost(surface);
7372 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7373 hr = IDirectDrawSurface7_Flip(surface, NULL, DDFLIP_WAIT);
7374 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7376 ret = SetForegroundWindow(window);
7377 ok(ret, "Failed to set foreground window.\n");
7378 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7379 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7380 hr = IDirectDrawSurface7_IsLost(surface);
7381 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7382 hr = IDirectDrawSurface7_Flip(surface, NULL, DDFLIP_WAIT);
7383 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7385 hr = IDirectDraw7_RestoreAllSurfaces(ddraw);
7386 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7387 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7388 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7389 hr = IDirectDrawSurface7_IsLost(surface);
7390 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7391 hr = IDirectDrawSurface7_Flip(surface, NULL, DDFLIP_WAIT);
7392 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7394 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7395 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7396 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7397 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7398 hr = IDirectDrawSurface7_IsLost(surface);
7399 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7400 hr = IDirectDrawSurface7_Flip(surface, NULL, DDFLIP_WAIT);
7401 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7403 /* Trying to restore the primary will crash, probably because flippable
7404 * surfaces can't exist in DDSCL_NORMAL. */
7405 IDirectDrawSurface7_Release(surface);
7406 memset(&surface_desc, 0, sizeof(surface_desc));
7407 surface_desc.dwSize = sizeof(surface_desc);
7408 surface_desc.dwFlags = DDSD_CAPS;
7409 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7410 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7411 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7413 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7414 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7415 hr = IDirectDrawSurface7_IsLost(surface);
7416 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7418 ret = SetForegroundWindow(GetDesktopWindow());
7419 ok(ret, "Failed to set foreground window.\n");
7420 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7421 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7422 hr = IDirectDrawSurface7_IsLost(surface);
7423 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7425 ret = SetForegroundWindow(window);
7426 ok(ret, "Failed to set foreground window.\n");
7427 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7428 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7429 hr = IDirectDrawSurface7_IsLost(surface);
7430 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7432 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7433 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7434 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7435 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7436 hr = IDirectDrawSurface7_IsLost(surface);
7437 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7439 hr = IDirectDraw7_RestoreAllSurfaces(ddraw);
7440 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7441 hr = IDirectDraw7_TestCooperativeLevel(ddraw);
7442 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7443 hr = IDirectDrawSurface7_IsLost(surface);
7444 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7446 IDirectDrawSurface7_Release(surface);
7447 refcount = IDirectDraw7_Release(ddraw);
7448 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7449 DestroyWindow(window);
7452 static void test_resource_priority(void)
7454 IDirectDrawSurface7 *surface, *mipmap;
7455 DDSURFACEDESC2 surface_desc;
7456 IDirectDraw7 *ddraw;
7457 ULONG refcount;
7458 HWND window;
7459 HRESULT hr;
7460 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, 0};
7461 DDCAPS hal_caps;
7462 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY | DDSCAPS_MIPMAP;
7463 unsigned int i;
7464 DWORD priority;
7465 static const struct
7467 DWORD caps, caps2;
7468 const char *name;
7469 HRESULT hr;
7470 /* SetPriority on offscreenplain surfaces crashes on AMD GPUs on Win7. */
7471 BOOL crash;
7473 test_data[] =
7475 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0, "vidmem texture", DDERR_INVALIDPARAMS, FALSE},
7476 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, "sysmem texture", DDERR_INVALIDPARAMS, FALSE},
7477 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, "managed texture", DD_OK, FALSE},
7478 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, "managed texture", DD_OK, FALSE},
7479 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0, "vidmem offscreenplain", DDERR_INVALIDOBJECT, TRUE},
7480 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0, "sysmem offscreenplain", DDERR_INVALIDOBJECT, TRUE},
7483 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7484 0, 0, 640, 480, 0, 0, 0, 0);
7485 ddraw = create_ddraw();
7486 ok(!!ddraw, "Failed to create a ddraw object.\n");
7487 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7488 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7490 memset(&hal_caps, 0, sizeof(hal_caps));
7491 hal_caps.dwSize = sizeof(hal_caps);
7492 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
7493 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7494 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps
7495 || !(hal_caps.ddsCaps.dwCaps & DDSCAPS2_TEXTUREMANAGE))
7497 skip("Required surface types not supported, skipping test.\n");
7498 goto done;
7501 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
7503 memset(&surface_desc, 0, sizeof(surface_desc));
7504 surface_desc.dwSize = sizeof(surface_desc);
7505 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
7506 surface_desc.dwWidth = 32;
7507 surface_desc.dwHeight = 32;
7508 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
7509 surface_desc.ddsCaps.dwCaps2 = test_data[i].caps2;
7510 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7511 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, type %s.\n", hr, test_data[i].name);
7513 /* Priority == NULL segfaults. */
7514 priority = 0xdeadbeef;
7515 hr = IDirectDrawSurface7_GetPriority(surface, &priority);
7516 ok(hr == test_data[i].hr, "Got unexpected hr %#x, type %s.\n", hr, test_data[i].name);
7517 if (SUCCEEDED(test_data[i].hr))
7518 ok(priority == 0, "Got unexpected priority %u, type %s.\n", priority, test_data[i].name);
7519 else
7520 ok(priority == 0xdeadbeef, "Got unexpected priority %u, type %s.\n", priority, test_data[i].name);
7522 if (!test_data[i].crash)
7524 hr = IDirectDrawSurface7_SetPriority(surface, 1);
7525 ok(hr == test_data[i].hr, "Got unexpected hr %#x, type %s.\n", hr, test_data[i].name);
7526 hr = IDirectDrawSurface7_GetPriority(surface, &priority);
7527 ok(hr == test_data[i].hr, "Got unexpected hr %#x, type %s.\n", hr, test_data[i].name);
7528 if (SUCCEEDED(test_data[i].hr))
7530 ok(priority == 1, "Got unexpected priority %u, type %s.\n", priority, test_data[i].name);
7531 hr = IDirectDrawSurface7_SetPriority(surface, 2);
7532 ok(hr == test_data[i].hr, "Got unexpected hr %#x, type %s.\n", hr, test_data[i].name);
7534 else
7535 ok(priority == 0xdeadbeef, "Got unexpected priority %u, type %s.\n", priority, test_data[i].name);
7538 IDirectDrawSurface7_Release(surface);
7541 memset(&surface_desc, 0, sizeof(surface_desc));
7542 surface_desc.dwSize = sizeof(surface_desc);
7543 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_MIPMAPCOUNT;
7544 surface_desc.dwWidth = 32;
7545 surface_desc.dwHeight = 32;
7546 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
7547 surface_desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
7548 U2(surface_desc).dwMipMapCount = 2;
7549 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7550 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7551 hr = IDirectDrawSurface7_GetAttachedSurface(surface, &caps, &mipmap);
7552 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
7554 priority = 0xdeadbeef;
7555 hr = IDirectDrawSurface7_GetPriority(mipmap, &priority);
7556 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x, type managed mipmap.\n", hr);
7557 ok(priority == 0xdeadbeef, "Got unexpected priority %u, type managed mipmap.\n", priority);
7558 /* SetPriority on the mipmap surface crashes. */
7559 hr = IDirectDrawSurface7_GetPriority(surface, &priority);
7560 ok(SUCCEEDED(hr), "Failed to get priority, hr %#x.\n", hr);
7561 ok(priority == 0, "Got unexpected priority %u, type managed mipmap.\n", priority);
7563 IDirectDrawSurface7_Release(mipmap);
7564 refcount = IDirectDrawSurface7_Release(surface);
7565 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7567 done:
7568 refcount = IDirectDraw7_Release(ddraw);
7569 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7570 DestroyWindow(window);
7573 static void test_surface_desc_lock(void)
7575 IDirectDrawSurface7 *surface;
7576 DDSURFACEDESC2 surface_desc;
7577 IDirectDraw7 *ddraw;
7578 ULONG refcount;
7579 HWND window;
7580 HRESULT hr;
7582 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7583 0, 0, 640, 480, 0, 0, 0, 0);
7584 ddraw = create_ddraw();
7585 ok(!!ddraw, "Failed to create a ddraw object.\n");
7586 hr = IDirectDraw7_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7587 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7589 memset(&surface_desc, 0, sizeof(surface_desc));
7590 surface_desc.dwSize = sizeof(surface_desc);
7591 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7592 surface_desc.dwWidth = 16;
7593 surface_desc.dwHeight = 16;
7594 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7595 hr = IDirectDraw7_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7596 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7598 memset(&surface_desc, 0xaa, sizeof(surface_desc));
7599 surface_desc.dwSize = sizeof(surface_desc);
7600 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &surface_desc);
7601 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
7602 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
7604 memset(&surface_desc, 0xaa, sizeof(surface_desc));
7605 surface_desc.dwSize = sizeof(surface_desc);
7606 hr = IDirectDrawSurface7_Lock(surface, NULL, &surface_desc, 0, NULL);
7607 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7608 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
7609 memset(&surface_desc, 0xaa, sizeof(surface_desc));
7610 surface_desc.dwSize = sizeof(surface_desc);
7611 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &surface_desc);
7612 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
7613 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
7614 hr = IDirectDrawSurface7_Unlock(surface, NULL);
7615 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
7617 memset(&surface_desc, 0xaa, sizeof(surface_desc));
7618 surface_desc.dwSize = sizeof(surface_desc);
7619 hr = IDirectDrawSurface7_GetSurfaceDesc(surface, &surface_desc);
7620 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
7621 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
7623 IDirectDrawSurface7_Release(surface);
7624 refcount = IDirectDraw7_Release(ddraw);
7625 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7626 DestroyWindow(window);
7629 START_TEST(ddraw7)
7631 HMODULE module = GetModuleHandleA("ddraw.dll");
7632 IDirectDraw7 *ddraw;
7634 if (!(pDirectDrawCreateEx = (void *)GetProcAddress(module, "DirectDrawCreateEx")))
7636 win_skip("DirectDrawCreateEx not available, skipping tests.\n");
7637 return;
7640 if (!(ddraw = create_ddraw()))
7642 skip("Failed to create a ddraw object, skipping tests.\n");
7643 return;
7645 IDirectDraw7_Release(ddraw);
7647 test_process_vertices();
7648 test_coop_level_create_device_window();
7649 test_clipper_blt();
7650 test_coop_level_d3d_state();
7651 test_surface_interface_mismatch();
7652 test_coop_level_threaded();
7653 test_depth_blit();
7654 test_texture_load_ckey();
7655 test_zenable();
7656 test_ck_rgba();
7657 test_ck_default();
7658 test_ck_complex();
7659 test_surface_qi();
7660 test_device_qi();
7661 test_wndproc();
7662 test_window_style();
7663 test_redundant_mode_set();
7664 test_coop_level_mode_set();
7665 test_coop_level_mode_set_multi();
7666 test_initialize();
7667 test_coop_level_surf_create();
7668 test_vb_discard();
7669 test_coop_level_multi_window();
7670 test_draw_strided();
7671 test_clear_rect_count();
7672 test_coop_level_versions();
7673 test_fog_special();
7674 test_lighting_interface_versions();
7675 test_coop_level_activateapp();
7676 test_texturemanage();
7677 test_block_formats_creation();
7678 test_unsupported_formats();
7679 test_rt_caps();
7680 test_primary_caps();
7681 test_surface_lock();
7682 test_surface_discard();
7683 test_flip();
7684 test_set_surface_desc();
7685 test_user_memory_getdc();
7686 test_sysmem_overlay();
7687 test_primary_palette();
7688 test_surface_attachment();
7689 test_private_data();
7690 test_pixel_format();
7691 test_create_surface_pitch();
7692 test_mipmap_lock();
7693 test_palette_complex();
7694 test_p8_rgb_blit();
7695 test_material();
7696 test_palette_gdi();
7697 test_palette_alpha();
7698 test_vb_writeonly();
7699 test_lost_device();
7700 test_resource_priority();
7701 test_surface_desc_lock();