wined3d: Implement color keying in the glsl fragment pipeline.
[wine.git] / dlls / ddraw / tests / ddraw1.c
blob597251cff8f92d0f3101a1eb09ed3a48ca3bfa0d
1 /*
2 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
3 * Copyright 2012-2013 Stefan Dösinger 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 "d3d.h"
24 static DEVMODEW registry_mode;
26 struct create_window_thread_param
28 HWND window;
29 HANDLE window_created;
30 HANDLE destroy_window;
31 HANDLE thread;
34 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
36 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
37 c1 >>= 8; c2 >>= 8;
38 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
39 c1 >>= 8; c2 >>= 8;
40 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
41 c1 >>= 8; c2 >>= 8;
42 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
43 return TRUE;
46 static DWORD WINAPI create_window_thread_proc(void *param)
48 struct create_window_thread_param *p = param;
49 DWORD res;
50 BOOL ret;
52 p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
53 0, 0, 640, 480, 0, 0, 0, 0);
54 ret = SetEvent(p->window_created);
55 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
57 for (;;)
59 MSG msg;
61 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
62 DispatchMessageA(&msg);
63 res = WaitForSingleObject(p->destroy_window, 100);
64 if (res == WAIT_OBJECT_0)
65 break;
66 if (res != WAIT_TIMEOUT)
68 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
69 break;
73 DestroyWindow(p->window);
75 return 0;
78 static void create_window_thread(struct create_window_thread_param *p)
80 DWORD res, tid;
82 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
83 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
84 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
85 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
86 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
87 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
88 res = WaitForSingleObject(p->window_created, INFINITE);
89 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
92 static void destroy_window_thread(struct create_window_thread_param *p)
94 SetEvent(p->destroy_window);
95 WaitForSingleObject(p->thread, INFINITE);
96 CloseHandle(p->destroy_window);
97 CloseHandle(p->window_created);
98 CloseHandle(p->thread);
101 static HRESULT set_display_mode(IDirectDraw *ddraw, DWORD width, DWORD height)
103 if (SUCCEEDED(IDirectDraw_SetDisplayMode(ddraw, width, height, 32)))
104 return DD_OK;
105 return IDirectDraw_SetDisplayMode(ddraw, width, height, 24);
108 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
110 RECT rect = {x, y, x + 1, y + 1};
111 DDSURFACEDESC surface_desc;
112 D3DCOLOR color;
113 HRESULT hr;
115 memset(&surface_desc, 0, sizeof(surface_desc));
116 surface_desc.dwSize = sizeof(surface_desc);
118 hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
119 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
120 if (FAILED(hr))
121 return 0xdeadbeef;
123 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
125 hr = IDirectDrawSurface_Unlock(surface, NULL);
126 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
128 return color;
131 static void emit_process_vertices(void **ptr, DWORD flags, WORD base_idx, DWORD vertex_count)
133 D3DINSTRUCTION *inst = *ptr;
134 D3DPROCESSVERTICES *pv = (D3DPROCESSVERTICES *)(inst + 1);
136 inst->bOpcode = D3DOP_PROCESSVERTICES;
137 inst->bSize = sizeof(*pv);
138 inst->wCount = 1;
140 pv->dwFlags = flags;
141 pv->wStart = base_idx;
142 pv->wDest = 0;
143 pv->dwCount = vertex_count;
144 pv->dwReserved = 0;
146 *ptr = pv + 1;
149 static void emit_set_ls(void **ptr, D3DLIGHTSTATETYPE state, DWORD value)
151 D3DINSTRUCTION *inst = *ptr;
152 D3DSTATE *ls = (D3DSTATE *)(inst + 1);
154 inst->bOpcode = D3DOP_STATELIGHT;
155 inst->bSize = sizeof(*ls);
156 inst->wCount = 1;
158 U1(*ls).dlstLightStateType = state;
159 U2(*ls).dwArg[0] = value;
161 *ptr = ls + 1;
164 static void emit_set_rs(void **ptr, D3DRENDERSTATETYPE state, DWORD value)
166 D3DINSTRUCTION *inst = *ptr;
167 D3DSTATE *rs = (D3DSTATE *)(inst + 1);
169 inst->bOpcode = D3DOP_STATERENDER;
170 inst->bSize = sizeof(*rs);
171 inst->wCount = 1;
173 U1(*rs).drstRenderStateType = state;
174 U2(*rs).dwArg[0] = value;
176 *ptr = rs + 1;
179 static void emit_tquad(void **ptr, WORD base_idx)
181 D3DINSTRUCTION *inst = *ptr;
182 D3DTRIANGLE *tri = (D3DTRIANGLE *)(inst + 1);
184 inst->bOpcode = D3DOP_TRIANGLE;
185 inst->bSize = sizeof(*tri);
186 inst->wCount = 2;
188 U1(*tri).v1 = base_idx;
189 U2(*tri).v2 = base_idx + 1;
190 U3(*tri).v3 = base_idx + 2;
191 tri->wFlags = D3DTRIFLAG_START;
192 ++tri;
194 U1(*tri).v1 = base_idx + 2;
195 U2(*tri).v2 = base_idx + 1;
196 U3(*tri).v3 = base_idx + 3;
197 tri->wFlags = D3DTRIFLAG_ODD;
198 ++tri;
200 *ptr = tri;
203 static void emit_end(void **ptr)
205 D3DINSTRUCTION *inst = *ptr;
207 inst->bOpcode = D3DOP_EXIT;
208 inst->bSize = 0;
209 inst->wCount = 0;
211 *ptr = inst + 1;
214 static void set_execute_data(IDirect3DExecuteBuffer *execute_buffer, UINT vertex_count, UINT offset, UINT len)
216 D3DEXECUTEDATA exec_data;
217 HRESULT hr;
219 memset(&exec_data, 0, sizeof(exec_data));
220 exec_data.dwSize = sizeof(exec_data);
221 exec_data.dwVertexCount = vertex_count;
222 exec_data.dwInstructionOffset = offset;
223 exec_data.dwInstructionLength = len;
224 hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
225 ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
228 static DWORD get_device_z_depth(IDirect3DDevice *device)
230 DDSCAPS caps = {DDSCAPS_ZBUFFER};
231 IDirectDrawSurface *ds, *rt;
232 DDSURFACEDESC desc;
233 HRESULT hr;
235 if (FAILED(IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt)))
236 return 0;
238 hr = IDirectDrawSurface_GetAttachedSurface(rt, &caps, &ds);
239 IDirectDrawSurface_Release(rt);
240 if (FAILED(hr))
241 return 0;
243 desc.dwSize = sizeof(desc);
244 hr = IDirectDrawSurface_GetSurfaceDesc(ds, &desc);
245 IDirectDrawSurface_Release(ds);
246 if (FAILED(hr))
247 return 0;
249 return U2(desc).dwZBufferBitDepth;
252 static IDirectDraw *create_ddraw(void)
254 IDirectDraw *ddraw;
256 if (FAILED(DirectDrawCreate(NULL, &ddraw, NULL)))
257 return NULL;
259 return ddraw;
262 static IDirect3DDevice *create_device(IDirectDraw *ddraw, HWND window, DWORD coop_level)
264 static const DWORD z_depths[] = {32, 24, 16};
265 IDirectDrawSurface *surface, *ds;
266 IDirect3DDevice *device = NULL;
267 DDSURFACEDESC surface_desc;
268 unsigned int i;
269 HRESULT hr;
271 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, coop_level);
272 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
274 memset(&surface_desc, 0, sizeof(surface_desc));
275 surface_desc.dwSize = sizeof(surface_desc);
276 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
277 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
278 surface_desc.dwWidth = 640;
279 surface_desc.dwHeight = 480;
281 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
282 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
284 if (coop_level & DDSCL_NORMAL)
286 IDirectDrawClipper *clipper;
288 hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
289 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
290 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
291 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
292 hr = IDirectDrawSurface_SetClipper(surface, clipper);
293 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
294 IDirectDrawClipper_Release(clipper);
297 /* We used to use EnumDevices() for this, but it seems
298 * D3DDEVICEDESC.dwDeviceZBufferBitDepth only has a very casual
299 * relationship with reality. */
300 for (i = 0; i < sizeof(z_depths) / sizeof(*z_depths); ++i)
302 memset(&surface_desc, 0, sizeof(surface_desc));
303 surface_desc.dwSize = sizeof(surface_desc);
304 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
305 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
306 U2(surface_desc).dwZBufferBitDepth = z_depths[i];
307 surface_desc.dwWidth = 640;
308 surface_desc.dwHeight = 480;
309 if (FAILED(IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL)))
310 continue;
312 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
313 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
314 IDirectDrawSurface_Release(ds);
315 if (FAILED(hr))
316 continue;
318 if (SUCCEEDED(IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device)))
319 break;
321 IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
324 IDirectDrawSurface_Release(surface);
325 return device;
328 static IDirect3DViewport *create_viewport(IDirect3DDevice *device, UINT x, UINT y, UINT w, UINT h)
330 IDirect3DViewport *viewport;
331 D3DVIEWPORT vp;
332 IDirect3D *d3d;
333 HRESULT hr;
335 hr = IDirect3DDevice_GetDirect3D(device, &d3d);
336 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
337 hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
338 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
339 hr = IDirect3DDevice_AddViewport(device, viewport);
340 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
341 memset(&vp, 0, sizeof(vp));
342 vp.dwSize = sizeof(vp);
343 vp.dwX = x;
344 vp.dwY = y;
345 vp.dwWidth = w;
346 vp.dwHeight = h;
347 vp.dvScaleX = (float)w / 2.0f;
348 vp.dvScaleY = (float)h / 2.0f;
349 vp.dvMaxX = 1.0f;
350 vp.dvMaxY = 1.0f;
351 vp.dvMinZ = 0.0f;
352 vp.dvMaxZ = 1.0f;
353 hr = IDirect3DViewport_SetViewport(viewport, &vp);
354 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
355 IDirect3D_Release(d3d);
357 return viewport;
360 static void viewport_set_background(IDirect3DDevice *device, IDirect3DViewport *viewport,
361 IDirect3DMaterial *material)
363 D3DMATERIALHANDLE material_handle;
364 HRESULT hr;
366 hr = IDirect3DMaterial2_GetHandle(material, device, &material_handle);
367 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
368 hr = IDirect3DViewport2_SetBackground(viewport, material_handle);
369 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
372 static void destroy_viewport(IDirect3DDevice *device, IDirect3DViewport *viewport)
374 HRESULT hr;
376 hr = IDirect3DDevice_DeleteViewport(device, viewport);
377 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
378 IDirect3DViewport_Release(viewport);
381 static IDirect3DMaterial *create_material(IDirect3DDevice *device, D3DMATERIAL *mat)
383 IDirect3DMaterial *material;
384 IDirect3D *d3d;
385 HRESULT hr;
387 hr = IDirect3DDevice_GetDirect3D(device, &d3d);
388 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
389 hr = IDirect3D_CreateMaterial(d3d, &material, NULL);
390 ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
391 hr = IDirect3DMaterial_SetMaterial(material, mat);
392 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
393 IDirect3D_Release(d3d);
395 return material;
398 static IDirect3DMaterial *create_diffuse_material(IDirect3DDevice *device, float r, float g, float b, float a)
400 D3DMATERIAL mat;
402 memset(&mat, 0, sizeof(mat));
403 mat.dwSize = sizeof(mat);
404 U1(U(mat).diffuse).r = r;
405 U2(U(mat).diffuse).g = g;
406 U3(U(mat).diffuse).b = b;
407 U4(U(mat).diffuse).a = a;
409 return create_material(device, &mat);
412 static IDirect3DMaterial *create_emissive_material(IDirect3DDevice *device, float r, float g, float b, float a)
414 D3DMATERIAL mat;
416 memset(&mat, 0, sizeof(mat));
417 mat.dwSize = sizeof(mat);
418 U1(U3(mat).emissive).r = r;
419 U2(U3(mat).emissive).g = g;
420 U3(U3(mat).emissive).b = b;
421 U4(U3(mat).emissive).a = a;
423 return create_material(device, &mat);
426 static void destroy_material(IDirect3DMaterial *material)
428 IDirect3DMaterial_Release(material);
431 struct message
433 UINT message;
434 BOOL check_wparam;
435 WPARAM expect_wparam;
438 static const struct message *expect_messages;
440 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
442 if (expect_messages && message == expect_messages->message)
444 if (expect_messages->check_wparam)
445 ok (wparam == expect_messages->expect_wparam,
446 "Got unexpected wparam %lx for message %x, expected %lx.\n",
447 wparam, message, expect_messages->expect_wparam);
449 ++expect_messages;
452 return DefWindowProcA(hwnd, message, wparam, lparam);
455 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
456 * interface. This prevents subsequent SetCooperativeLevel() calls on a
457 * different window from failing with DDERR_HWNDALREADYSET. */
458 static void fix_wndproc(HWND window, LONG_PTR proc)
460 IDirectDraw *ddraw;
461 HRESULT hr;
463 if (!(ddraw = create_ddraw()))
464 return;
466 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
467 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
468 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
469 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
470 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
472 IDirectDraw_Release(ddraw);
475 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
477 HRESULT hr = IDirectDrawSurface_Restore(surface);
478 ok(SUCCEEDED(hr) || hr == DDERR_IMPLICITLYCREATED, "Failed to restore surface, hr %#x.\n", hr);
479 IDirectDrawSurface_Release(surface);
481 return DDENUMRET_OK;
484 static HRESULT restore_surfaces(IDirectDraw *ddraw)
486 return IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
487 NULL, NULL, restore_callback);
490 static void test_coop_level_create_device_window(void)
492 HWND focus_window, device_window;
493 IDirectDraw *ddraw;
494 HRESULT hr;
496 focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
497 0, 0, 640, 480, 0, 0, 0, 0);
498 ddraw = create_ddraw();
499 ok(!!ddraw, "Failed to create a ddraw object.\n");
501 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
502 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
503 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
504 ok(!device_window, "Unexpected device window found.\n");
505 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
506 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
507 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
508 ok(!device_window, "Unexpected device window found.\n");
509 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
510 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
511 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
512 ok(!device_window, "Unexpected device window found.\n");
513 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
514 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
515 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
516 ok(!device_window, "Unexpected device window found.\n");
517 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
518 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
519 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
520 ok(!device_window, "Unexpected device window found.\n");
522 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
523 if (broken(hr == DDERR_INVALIDPARAMS))
525 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
526 IDirectDraw_Release(ddraw);
527 DestroyWindow(focus_window);
528 return;
531 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
532 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
533 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
534 ok(!device_window, "Unexpected device window found.\n");
535 hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
536 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
537 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
538 ok(!device_window, "Unexpected device window found.\n");
540 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
541 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
542 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
543 ok(!device_window, "Unexpected device window found.\n");
544 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
545 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
546 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
547 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
548 ok(!!device_window, "Device window not found.\n");
550 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
551 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
552 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
553 ok(!device_window, "Unexpected device window found.\n");
554 hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
555 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
556 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
557 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
558 ok(!!device_window, "Device window not found.\n");
560 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
561 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
562 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
563 ok(!device_window, "Unexpected device window found.\n");
564 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
565 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
566 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
567 ok(!device_window, "Unexpected device window found.\n");
568 hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
569 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
570 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
571 ok(!device_window, "Unexpected device window found.\n");
572 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
573 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
574 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
575 ok(!!device_window, "Device window not found.\n");
577 IDirectDraw_Release(ddraw);
578 DestroyWindow(focus_window);
581 static void test_clipper_blt(void)
583 IDirectDrawSurface *src_surface, *dst_surface;
584 RECT client_rect, src_rect;
585 IDirectDrawClipper *clipper;
586 DDSURFACEDESC surface_desc;
587 unsigned int i, j, x, y;
588 IDirectDraw *ddraw;
589 RGNDATA *rgn_data;
590 D3DCOLOR color;
591 HRGN r1, r2;
592 HWND window;
593 DDBLTFX fx;
594 HRESULT hr;
595 DWORD *ptr;
596 DWORD ret;
598 static const DWORD src_data[] =
600 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
601 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
602 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
604 static const D3DCOLOR expected1[] =
606 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
607 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
608 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
609 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
611 /* Nvidia on Windows seems to have an off-by-one error
612 * when processing source rectangles. Our left = 1 and
613 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
614 * read as well, but only for the edge pixels on the
615 * output image. The bug happens on the y axis as well,
616 * but we only read one row there, and all source rows
617 * contain the same data. This bug is not dependent on
618 * the presence of a clipper. */
619 static const D3DCOLOR expected1_broken[] =
621 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
622 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
623 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
624 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
626 static const D3DCOLOR expected2[] =
628 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
629 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
630 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
631 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
634 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
635 10, 10, 640, 480, 0, 0, 0, 0);
636 ShowWindow(window, SW_SHOW);
637 ddraw = create_ddraw();
638 ok(!!ddraw, "Failed to create a ddraw object.\n");
640 ret = GetClientRect(window, &client_rect);
641 ok(ret, "Failed to get client rect.\n");
642 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
643 ok(ret, "Failed to map client rect.\n");
645 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
646 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
648 hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
649 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
650 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
651 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
652 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
653 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
654 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
655 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
656 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
657 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
658 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
659 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
660 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
661 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
662 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
663 "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
664 rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
665 rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
666 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
667 HeapFree(GetProcessHeap(), 0, rgn_data);
669 r1 = CreateRectRgn(0, 0, 320, 240);
670 ok(!!r1, "Failed to create region.\n");
671 r2 = CreateRectRgn(320, 240, 640, 480);
672 ok(!!r2, "Failed to create region.\n");
673 CombineRgn(r1, r1, r2, RGN_OR);
674 ret = GetRegionData(r1, 0, NULL);
675 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
676 ret = GetRegionData(r1, ret, rgn_data);
677 ok(!!ret, "Failed to get region data.\n");
679 DeleteObject(r2);
680 DeleteObject(r1);
682 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
683 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
684 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
685 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
686 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
687 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
689 HeapFree(GetProcessHeap(), 0, rgn_data);
691 memset(&surface_desc, 0, sizeof(surface_desc));
692 surface_desc.dwSize = sizeof(surface_desc);
693 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
694 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
695 surface_desc.dwWidth = 640;
696 surface_desc.dwHeight = 480;
697 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
698 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
699 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
700 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
701 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
702 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
704 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
705 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
706 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
707 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
709 memset(&fx, 0, sizeof(fx));
710 fx.dwSize = sizeof(fx);
711 hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
712 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
713 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
714 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
716 hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
717 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
718 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
719 ptr = surface_desc.lpSurface;
720 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
721 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
722 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
723 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
724 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
726 hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
727 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
729 SetRect(&src_rect, 1, 1, 5, 2);
730 hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
731 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
732 for (i = 0; i < 4; ++i)
734 for (j = 0; j < 4; ++j)
736 x = 80 * ((2 * j) + 1);
737 y = 60 * ((2 * i) + 1);
738 color = get_surface_color(dst_surface, x, y);
739 ok(compare_color(color, expected1[i * 4 + j], 1)
740 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
741 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
745 U5(fx).dwFillColor = 0xff0000ff;
746 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
747 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
748 for (i = 0; i < 4; ++i)
750 for (j = 0; j < 4; ++j)
752 x = 80 * ((2 * j) + 1);
753 y = 60 * ((2 * i) + 1);
754 color = get_surface_color(dst_surface, x, y);
755 ok(compare_color(color, expected2[i * 4 + j], 1),
756 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
760 hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
761 ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
763 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
764 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
765 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
766 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
767 DestroyWindow(window);
768 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
769 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
770 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
771 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
772 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
773 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
774 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
775 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
776 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
777 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
778 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
779 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
781 IDirectDrawSurface_Release(dst_surface);
782 IDirectDrawSurface_Release(src_surface);
783 IDirectDrawClipper_Release(clipper);
784 IDirectDraw_Release(ddraw);
787 static void test_coop_level_d3d_state(void)
789 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
790 IDirectDrawSurface *rt, *surface;
791 IDirect3DMaterial *background;
792 IDirect3DViewport *viewport;
793 IDirect3DDevice *device;
794 D3DMATERIAL material;
795 IDirectDraw *ddraw;
796 D3DCOLOR color;
797 HWND window;
798 HRESULT hr;
800 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
801 0, 0, 640, 480, 0, 0, 0, 0);
802 ddraw = create_ddraw();
803 ok(!!ddraw, "Failed to create a ddraw object.\n");
804 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
806 skip("Failed to create a 3D device, skipping test.\n");
807 IDirectDraw_Release(ddraw);
808 DestroyWindow(window);
809 return;
812 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
813 viewport = create_viewport(device, 0, 0, 640, 480);
814 viewport_set_background(device, viewport, background);
816 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
817 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
818 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
819 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
820 color = get_surface_color(rt, 320, 240);
821 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
823 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
824 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
825 hr = IDirectDrawSurface_IsLost(rt);
826 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
827 hr = restore_surfaces(ddraw);
828 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
830 memset(&material, 0, sizeof(material));
831 material.dwSize = sizeof(material);
832 U1(U(material).diffuse).r = 0.0f;
833 U2(U(material).diffuse).g = 1.0f;
834 U3(U(material).diffuse).b = 0.0f;
835 U4(U(material).diffuse).a = 1.0f;
836 hr = IDirect3DMaterial_SetMaterial(background, &material);
837 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
839 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&surface);
840 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
841 ok(surface == rt, "Got unexpected surface %p.\n", surface);
842 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
843 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
844 color = get_surface_color(rt, 320, 240);
845 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
847 destroy_viewport(device, viewport);
848 destroy_material(background);
849 IDirectDrawSurface_Release(surface);
850 IDirectDrawSurface_Release(rt);
851 IDirect3DDevice_Release(device);
852 IDirectDraw_Release(ddraw);
853 DestroyWindow(window);
856 static void test_surface_interface_mismatch(void)
858 IDirectDraw *ddraw = NULL;
859 IDirectDrawSurface *surface = NULL, *ds;
860 IDirectDrawSurface3 *surface3 = NULL;
861 IDirect3DDevice *device = NULL;
862 IDirect3DViewport *viewport = NULL;
863 IDirect3DMaterial *background = NULL;
864 DDSURFACEDESC surface_desc;
865 DWORD z_depth = 0;
866 ULONG refcount;
867 HRESULT hr;
868 D3DCOLOR color;
869 HWND window;
870 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
872 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
873 0, 0, 640, 480, 0, 0, 0, 0);
874 ddraw = create_ddraw();
875 ok(!!ddraw, "Failed to create a ddraw object.\n");
876 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
878 skip("Failed to create a 3D device, skipping test.\n");
879 IDirectDraw_Release(ddraw);
880 DestroyWindow(window);
881 return;
883 z_depth = get_device_z_depth(device);
884 ok(!!z_depth, "Failed to get device z depth.\n");
885 IDirect3DDevice_Release(device);
886 device = NULL;
888 memset(&surface_desc, 0, sizeof(surface_desc));
889 surface_desc.dwSize = sizeof(surface_desc);
890 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
891 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
892 surface_desc.dwWidth = 640;
893 surface_desc.dwHeight = 480;
895 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
896 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
898 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
899 if (FAILED(hr))
901 skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
902 goto cleanup;
905 memset(&surface_desc, 0, sizeof(surface_desc));
906 surface_desc.dwSize = sizeof(surface_desc);
907 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
908 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
909 U2(surface_desc).dwZBufferBitDepth = z_depth;
910 surface_desc.dwWidth = 640;
911 surface_desc.dwHeight = 480;
912 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
913 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
914 if (FAILED(hr))
915 goto cleanup;
917 /* Using a different surface interface version still works */
918 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
919 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
920 refcount = IDirectDrawSurface_Release(ds);
921 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
922 if (FAILED(hr))
923 goto cleanup;
925 /* Here too */
926 hr = IDirectDrawSurface3_QueryInterface(surface3, &IID_IDirect3DHALDevice, (void **)&device);
927 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
928 if (FAILED(hr))
929 goto cleanup;
931 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
932 viewport = create_viewport(device, 0, 0, 640, 480);
933 viewport_set_background(device, viewport, background);
935 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
936 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
937 color = get_surface_color(surface, 320, 240);
938 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
940 cleanup:
941 if (viewport)
942 destroy_viewport(device, viewport);
943 if (background)
944 destroy_material(background);
945 if (surface3) IDirectDrawSurface3_Release(surface3);
946 if (surface) IDirectDrawSurface_Release(surface);
947 if (device) IDirect3DDevice_Release(device);
948 if (ddraw) IDirectDraw_Release(ddraw);
949 DestroyWindow(window);
952 static void test_coop_level_threaded(void)
954 struct create_window_thread_param p;
955 IDirectDraw *ddraw;
956 HRESULT hr;
958 ddraw = create_ddraw();
959 ok(!!ddraw, "Failed to create a ddraw object.\n");
960 create_window_thread(&p);
962 hr = IDirectDraw_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
963 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
965 IDirectDraw_Release(ddraw);
966 destroy_window_thread(&p);
969 static ULONG get_refcount(IUnknown *test_iface)
971 IUnknown_AddRef(test_iface);
972 return IUnknown_Release(test_iface);
975 static void test_viewport(void)
977 IDirectDraw *ddraw;
978 IDirect3D *d3d;
979 HRESULT hr;
980 ULONG ref;
981 IDirect3DViewport *viewport, *another_vp;
982 IDirect3DViewport2 *viewport2;
983 IDirect3DViewport3 *viewport3;
984 IDirectDrawGammaControl *gamma;
985 IUnknown *unknown;
986 IDirect3DDevice *device;
987 HWND window;
989 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
990 0, 0, 640, 480, 0, 0, 0, 0);
991 ddraw = create_ddraw();
992 ok(!!ddraw, "Failed to create a ddraw object.\n");
993 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
995 skip("Failed to create a 3D device, skipping test.\n");
996 IDirectDraw_Release(ddraw);
997 DestroyWindow(window);
998 return;
1001 hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
1002 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1003 ref = get_refcount((IUnknown *) d3d);
1004 ok(ref == 2, "IDirect3D refcount is %d\n", ref);
1006 hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
1007 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1008 ref = get_refcount((IUnknown *)viewport);
1009 ok(ref == 1, "Initial IDirect3DViewport refcount is %u\n", ref);
1010 ref = get_refcount((IUnknown *)d3d);
1011 ok(ref == 2, "IDirect3D refcount is %u\n", ref);
1013 /* E_FAIL return values are returned by Winetestbot Windows NT machines. While not supporting
1014 * newer interfaces is legitimate for old ddraw versions, E_FAIL violates Microsoft's rules
1015 * for QueryInterface, hence the broken() */
1016 gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1017 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirectDrawGammaControl, (void **)&gamma);
1018 ok(hr == E_NOINTERFACE || broken(hr == E_FAIL), "Got unexpected hr %#x.\n", hr);
1019 ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1020 if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1021 /* NULL iid: Segfaults */
1023 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport2, (void **)&viewport2);
1024 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
1025 "Failed to QI IDirect3DViewport2, hr %#x.\n", hr);
1026 if (viewport2)
1028 ref = get_refcount((IUnknown *)viewport);
1029 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1030 ref = get_refcount((IUnknown *)viewport2);
1031 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1032 IDirect3DViewport2_Release(viewport2);
1033 viewport2 = NULL;
1036 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport3, (void **)&viewport3);
1037 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
1038 "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1039 if (viewport3)
1041 ref = get_refcount((IUnknown *)viewport);
1042 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1043 ref = get_refcount((IUnknown *)viewport3);
1044 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1045 IDirect3DViewport3_Release(viewport3);
1048 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IUnknown, (void **)&unknown);
1049 ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1050 if (unknown)
1052 ref = get_refcount((IUnknown *)viewport);
1053 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1054 ref = get_refcount(unknown);
1055 ok(ref == 2, "IUnknown refcount is %u\n", ref);
1056 IUnknown_Release(unknown);
1059 /* AddViewport(NULL): Segfault */
1060 hr = IDirect3DDevice_DeleteViewport(device, NULL);
1061 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1063 hr = IDirect3D_CreateViewport(d3d, &another_vp, NULL);
1064 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1066 hr = IDirect3DDevice_AddViewport(device, viewport);
1067 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1068 ref = get_refcount((IUnknown *) viewport);
1069 ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
1070 hr = IDirect3DDevice_AddViewport(device, another_vp);
1071 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1072 ref = get_refcount((IUnknown *) another_vp);
1073 ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
1075 hr = IDirect3DDevice_DeleteViewport(device, another_vp);
1076 ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1077 ref = get_refcount((IUnknown *) another_vp);
1078 ok(ref == 1, "IDirect3DViewport refcount is %d\n", ref);
1080 IDirect3DDevice_Release(device);
1081 ref = get_refcount((IUnknown *) viewport);
1082 ok(ref == 1, "IDirect3DViewport refcount is %d\n", ref);
1084 IDirect3DViewport_Release(another_vp);
1085 IDirect3D_Release(d3d);
1086 IDirect3DViewport_Release(viewport);
1087 DestroyWindow(window);
1088 IDirectDraw_Release(ddraw);
1091 static void test_zenable(void)
1093 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1094 static D3DTLVERTEX tquad[] =
1096 {{ 0.0f}, {480.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1097 {{ 0.0f}, { 0.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1098 {{640.0f}, {480.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1099 {{640.0f}, { 0.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1101 IDirect3DExecuteBuffer *execute_buffer;
1102 D3DEXECUTEBUFFERDESC exec_desc;
1103 IDirect3DMaterial *background;
1104 IDirect3DViewport *viewport;
1105 IDirect3DDevice *device;
1106 IDirectDrawSurface *rt;
1107 IDirectDraw *ddraw;
1108 UINT inst_length;
1109 D3DCOLOR color;
1110 HWND window;
1111 HRESULT hr;
1112 UINT x, y;
1113 UINT i, j;
1114 void *ptr;
1116 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1117 0, 0, 640, 480, 0, 0, 0, 0);
1118 ddraw = create_ddraw();
1119 ok(!!ddraw, "Failed to create a ddraw object.\n");
1120 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1122 skip("Failed to create a 3D device, skipping test.\n");
1123 IDirectDraw_Release(ddraw);
1124 DestroyWindow(window);
1125 return;
1128 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1129 viewport = create_viewport(device, 0, 0, 640, 480);
1130 viewport_set_background(device, viewport, background);
1132 memset(&exec_desc, 0, sizeof(exec_desc));
1133 exec_desc.dwSize = sizeof(exec_desc);
1134 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1135 exec_desc.dwBufferSize = 1024;
1136 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1138 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1139 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1140 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1141 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1142 memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1143 ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1144 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1145 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1146 emit_tquad(&ptr, 0);
1147 emit_end(&ptr);
1148 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
1149 inst_length -= sizeof(tquad);
1150 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1151 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1153 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1154 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1155 hr = IDirect3DDevice_BeginScene(device);
1156 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1157 set_execute_data(execute_buffer, 4, sizeof(tquad), inst_length);
1158 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1159 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1160 hr = IDirect3DDevice_EndScene(device);
1161 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1163 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1164 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1165 for (i = 0; i < 4; ++i)
1167 for (j = 0; j < 4; ++j)
1169 x = 80 * ((2 * j) + 1);
1170 y = 60 * ((2 * i) + 1);
1171 color = get_surface_color(rt, x, y);
1172 ok(compare_color(color, 0x0000ff00, 1),
1173 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1176 IDirectDrawSurface_Release(rt);
1178 destroy_viewport(device, viewport);
1179 IDirect3DExecuteBuffer_Release(execute_buffer);
1180 destroy_material(background);
1181 IDirect3DDevice_Release(device);
1182 IDirectDraw_Release(ddraw);
1183 DestroyWindow(window);
1186 static void test_ck_rgba(void)
1188 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1189 static D3DTLVERTEX tquad[] =
1191 {{ 0.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1192 {{ 0.0f}, { 0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1193 {{640.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1194 {{640.0f}, { 0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1195 {{ 0.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1196 {{ 0.0f}, { 0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1197 {{640.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1198 {{640.0f}, { 0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1200 static const struct
1202 D3DCOLOR fill_color;
1203 BOOL color_key;
1204 BOOL blend;
1205 D3DCOLOR result1, result1_broken;
1206 D3DCOLOR result2, result2_broken;
1208 tests[] =
1210 /* r200 on Windows doesn't check the alpha component when applying the color
1211 * key, so the key matches on every texel. */
1212 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1213 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x00ff0000, 0x000000ff, 0x000000ff},
1214 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1215 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1216 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00ff0000, 0x00807f00, 0x000000ff},
1217 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x00ff0000, 0x0000ff00, 0x000000ff},
1218 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00, 0x00807f00, 0x00807f00},
1219 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1222 IDirect3DExecuteBuffer *execute_buffer;
1223 D3DTEXTUREHANDLE texture_handle;
1224 D3DEXECUTEBUFFERDESC exec_desc;
1225 IDirect3DMaterial *background;
1226 IDirectDrawSurface *surface;
1227 IDirect3DViewport *viewport;
1228 DDSURFACEDESC surface_desc;
1229 IDirect3DTexture *texture;
1230 IDirect3DDevice *device;
1231 IDirectDrawSurface *rt;
1232 IDirectDraw *ddraw;
1233 D3DCOLOR color;
1234 HWND window;
1235 DDBLTFX fx;
1236 HRESULT hr;
1237 UINT i;
1239 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1240 0, 0, 640, 480, 0, 0, 0, 0);
1241 ddraw = create_ddraw();
1242 ok(!!ddraw, "Failed to create a ddraw object.\n");
1243 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1245 skip("Failed to create a 3D device, skipping test.\n");
1246 IDirectDraw_Release(ddraw);
1247 DestroyWindow(window);
1248 return;
1251 background = create_diffuse_material(device, 1.0, 0.0f, 0.0f, 1.0f);
1252 viewport = create_viewport(device, 0, 0, 640, 480);
1253 viewport_set_background(device, viewport, background);
1255 memset(&surface_desc, 0, sizeof(surface_desc));
1256 surface_desc.dwSize = sizeof(surface_desc);
1257 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1258 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1259 surface_desc.dwWidth = 256;
1260 surface_desc.dwHeight = 256;
1261 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1262 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1263 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1264 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1265 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1266 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1267 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1268 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1269 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1270 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1271 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1272 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1273 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1274 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1275 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1276 IDirect3DTexture_Release(texture);
1278 memset(&exec_desc, 0, sizeof(exec_desc));
1279 exec_desc.dwSize = sizeof(exec_desc);
1280 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1281 exec_desc.dwBufferSize = 1024;
1282 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1283 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1284 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1286 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1287 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1289 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1291 UINT draw1_len, draw2_len;
1292 void *ptr;
1294 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1295 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1296 memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1297 ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1298 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1299 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1300 emit_set_rs(&ptr, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1301 emit_set_rs(&ptr, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1302 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1303 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1304 emit_tquad(&ptr, 0);
1305 emit_end(&ptr);
1306 draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - sizeof(tquad);
1307 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 4, 4);
1308 emit_tquad(&ptr, 0);
1309 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1310 emit_end(&ptr);
1311 draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_len;
1312 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1313 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1315 memset(&fx, 0, sizeof(fx));
1316 fx.dwSize = sizeof(fx);
1317 U5(fx).dwFillColor = tests[i].fill_color;
1318 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1319 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1321 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
1322 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1323 hr = IDirect3DDevice_BeginScene(device);
1324 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1325 set_execute_data(execute_buffer, 8, sizeof(tquad), draw1_len);
1326 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1327 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1328 hr = IDirect3DDevice_EndScene(device);
1329 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1331 color = get_surface_color(rt, 320, 240);
1332 ok(compare_color(color, tests[i].result1, 1) || compare_color(color, tests[i].result1_broken, 1),
1333 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1334 tests[i].result1, i, color);
1336 U5(fx).dwFillColor = 0xff0000ff;
1337 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1338 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1340 hr = IDirect3DDevice_BeginScene(device);
1341 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1342 set_execute_data(execute_buffer, 8, sizeof(tquad) + draw1_len, draw2_len);
1343 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1344 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1345 hr = IDirect3DDevice_EndScene(device);
1346 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1348 /* This tests that fragments that are masked out by the color key are
1349 * discarded, instead of just fully transparent. */
1350 color = get_surface_color(rt, 320, 240);
1351 ok(compare_color(color, tests[i].result2, 1) || compare_color(color, tests[i].result2_broken, 1),
1352 "Expected color 0x%08x for test %u, got 0x%08x.\n",
1353 tests[i].result2, i, color);
1356 IDirectDrawSurface_Release(rt);
1357 IDirect3DExecuteBuffer_Release(execute_buffer);
1358 IDirectDrawSurface_Release(surface);
1359 destroy_viewport(device, viewport);
1360 destroy_material(background);
1361 IDirect3DDevice_Release(device);
1362 IDirectDraw_Release(ddraw);
1363 DestroyWindow(window);
1366 static void test_ck_default(void)
1368 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1369 static D3DTLVERTEX tquad[] =
1371 {{ 0.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1372 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1373 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1374 {{640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1376 IDirect3DExecuteBuffer *execute_buffer;
1377 IDirectDrawSurface *surface, *rt;
1378 D3DTEXTUREHANDLE texture_handle;
1379 D3DEXECUTEBUFFERDESC exec_desc;
1380 IDirect3DMaterial *background;
1381 UINT draw1_offset, draw1_len;
1382 UINT draw2_offset, draw2_len;
1383 UINT draw3_offset, draw3_len;
1384 UINT draw4_offset, draw4_len;
1385 IDirect3DViewport *viewport;
1386 DDSURFACEDESC surface_desc;
1387 IDirect3DTexture *texture;
1388 IDirect3DDevice *device;
1389 IDirectDraw *ddraw;
1390 D3DCOLOR color;
1391 HWND window;
1392 DDBLTFX fx;
1393 HRESULT hr;
1394 void *ptr;
1396 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1397 0, 0, 640, 480, 0, 0, 0, 0);
1398 ddraw = create_ddraw();
1399 ok(!!ddraw, "Failed to create a ddraw object.\n");
1400 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1402 skip("Failed to create a 3D device, skipping test.\n");
1403 IDirectDraw_Release(ddraw);
1404 DestroyWindow(window);
1405 return;
1408 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1409 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1411 background = create_diffuse_material(device, 0.0, 1.0f, 0.0f, 1.0f);
1412 viewport = create_viewport(device, 0, 0, 640, 480);
1413 viewport_set_background(device, viewport, background);
1415 memset(&surface_desc, 0, sizeof(surface_desc));
1416 surface_desc.dwSize = sizeof(surface_desc);
1417 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1418 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1419 surface_desc.dwWidth = 256;
1420 surface_desc.dwHeight = 256;
1421 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1422 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1423 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1424 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1425 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1426 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1427 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1428 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1429 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1430 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1431 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1432 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1433 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1434 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1435 IDirect3DTexture_Release(texture);
1437 memset(&fx, 0, sizeof(fx));
1438 fx.dwSize = sizeof(fx);
1439 U5(fx).dwFillColor = 0x000000ff;
1440 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1441 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1443 memset(&exec_desc, 0, sizeof(exec_desc));
1444 exec_desc.dwSize = sizeof(exec_desc);
1445 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1446 exec_desc.dwBufferSize = 1024;
1447 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1448 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1449 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1451 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1452 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1453 memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1454 ptr = (BYTE *)exec_desc.lpData + sizeof(tquad);
1455 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1456 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1457 emit_tquad(&ptr, 0);
1458 emit_end(&ptr);
1459 draw1_offset = sizeof(tquad);
1460 draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_offset;
1461 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1462 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, FALSE);
1463 emit_tquad(&ptr, 0);
1464 emit_end(&ptr);
1465 draw2_offset = draw1_offset + draw1_len;
1466 draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw2_offset;
1467 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1468 emit_tquad(&ptr, 0);
1469 emit_end(&ptr);
1470 draw3_offset = draw2_offset + draw2_len;
1471 draw3_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw3_offset;
1472 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1473 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1474 emit_tquad(&ptr, 0);
1475 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1476 emit_end(&ptr);
1477 draw4_offset = draw3_offset + draw3_len;
1478 draw4_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw4_offset;
1479 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1480 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1482 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1483 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1484 hr = IDirect3DDevice_BeginScene(device);
1485 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1486 set_execute_data(execute_buffer, 4, draw1_offset, draw1_len);
1487 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1488 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1489 hr = IDirect3DDevice_EndScene(device);
1490 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1491 color = get_surface_color(rt, 320, 240);
1492 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1494 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1495 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1496 hr = IDirect3DDevice_BeginScene(device);
1497 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1498 set_execute_data(execute_buffer, 4, draw2_offset, draw2_len);
1499 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1500 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1501 hr = IDirect3DDevice_EndScene(device);
1502 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1503 color = get_surface_color(rt, 320, 240);
1504 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1506 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1507 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1508 hr = IDirect3DDevice_BeginScene(device);
1509 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1510 set_execute_data(execute_buffer, 4, draw3_offset, draw3_len);
1511 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1512 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1513 hr = IDirect3DDevice_EndScene(device);
1514 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1515 color = get_surface_color(rt, 320, 240);
1516 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1518 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1519 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1520 hr = IDirect3DDevice_BeginScene(device);
1521 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1522 set_execute_data(execute_buffer, 4, draw4_offset, draw4_len);
1523 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1524 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1525 hr = IDirect3DDevice_EndScene(device);
1526 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1527 color = get_surface_color(rt, 320, 240);
1528 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1530 IDirect3DExecuteBuffer_Release(execute_buffer);
1531 IDirectDrawSurface_Release(surface);
1532 destroy_viewport(device, viewport);
1533 destroy_material(background);
1534 IDirectDrawSurface_Release(rt);
1535 IDirect3DDevice_Release(device);
1536 IDirectDraw_Release(ddraw);
1537 DestroyWindow(window);
1540 static void test_ck_complex(void)
1542 IDirectDrawSurface *surface, *mipmap, *tmp;
1543 DDSCAPS caps = {DDSCAPS_COMPLEX};
1544 DDSURFACEDESC surface_desc;
1545 IDirect3DDevice *device;
1546 DDCOLORKEY color_key;
1547 IDirectDraw *ddraw;
1548 unsigned int i;
1549 ULONG refcount;
1550 HWND window;
1551 HRESULT hr;
1553 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1554 0, 0, 640, 480, 0, 0, 0, 0);
1555 ddraw = create_ddraw();
1556 ok(!!ddraw, "Failed to create a ddraw object.\n");
1557 if (!(device = create_device(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1559 skip("Failed to create a 3D device, skipping test.\n");
1560 DestroyWindow(window);
1561 IDirectDraw2_Release(ddraw);
1562 return;
1564 IDirect3DDevice_Release(device);
1566 memset(&surface_desc, 0, sizeof(surface_desc));
1567 surface_desc.dwSize = sizeof(surface_desc);
1568 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1569 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1570 surface_desc.dwWidth = 128;
1571 surface_desc.dwHeight = 128;
1572 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1573 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1575 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1576 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1577 color_key.dwColorSpaceLowValue = 0x0000ff00;
1578 color_key.dwColorSpaceHighValue = 0x0000ff00;
1579 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1580 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1581 memset(&color_key, 0, sizeof(color_key));
1582 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1583 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1584 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1585 color_key.dwColorSpaceLowValue);
1586 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1587 color_key.dwColorSpaceHighValue);
1589 mipmap = surface;
1590 IDirectDrawSurface_AddRef(mipmap);
1591 for (i = 0; i < 7; ++i)
1593 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
1594 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
1596 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1597 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1598 color_key.dwColorSpaceLowValue = 0x000000ff;
1599 color_key.dwColorSpaceHighValue = 0x000000ff;
1600 hr = IDirectDrawSurface_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1601 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x, i %u.\n", hr, i);
1602 memset(&color_key, 0, sizeof(color_key));
1603 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1604 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x, i %u.\n", hr, i);
1605 ok(color_key.dwColorSpaceLowValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1606 color_key.dwColorSpaceLowValue, i);
1607 ok(color_key.dwColorSpaceHighValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1608 color_key.dwColorSpaceHighValue, i);
1610 IDirectDrawSurface_Release(mipmap);
1611 mipmap = tmp;
1614 memset(&color_key, 0, sizeof(color_key));
1615 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1616 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1617 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1618 color_key.dwColorSpaceLowValue);
1619 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1620 color_key.dwColorSpaceHighValue);
1622 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
1623 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
1624 IDirectDrawSurface_Release(mipmap);
1625 refcount = IDirectDrawSurface_Release(surface);
1626 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1628 memset(&surface_desc, 0, sizeof(surface_desc));
1629 surface_desc.dwSize = sizeof(surface_desc);
1630 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
1631 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1632 surface_desc.dwBackBufferCount = 1;
1633 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1634 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1636 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1637 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1638 color_key.dwColorSpaceLowValue = 0x0000ff00;
1639 color_key.dwColorSpaceHighValue = 0x0000ff00;
1640 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1641 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1642 memset(&color_key, 0, sizeof(color_key));
1643 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1644 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1645 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1646 color_key.dwColorSpaceLowValue);
1647 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1648 color_key.dwColorSpaceHighValue);
1650 hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &tmp);
1651 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
1653 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1654 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1655 color_key.dwColorSpaceLowValue = 0x0000ff00;
1656 color_key.dwColorSpaceHighValue = 0x0000ff00;
1657 hr = IDirectDrawSurface_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1658 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1659 memset(&color_key, 0, sizeof(color_key));
1660 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1661 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1662 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1663 color_key.dwColorSpaceLowValue);
1664 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1665 color_key.dwColorSpaceHighValue);
1667 IDirectDrawSurface_Release(tmp);
1669 refcount = IDirectDrawSurface_Release(surface);
1670 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1671 refcount = IDirectDraw_Release(ddraw);
1672 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1673 DestroyWindow(window);
1676 struct qi_test
1678 REFIID iid;
1679 REFIID refcount_iid;
1680 HRESULT hr;
1683 static void test_qi(const char *test_name, IUnknown *base_iface,
1684 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
1686 ULONG refcount, expected_refcount;
1687 IUnknown *iface1, *iface2;
1688 HRESULT hr;
1689 UINT i, j;
1691 for (i = 0; i < entry_count; ++i)
1693 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
1694 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
1695 if (SUCCEEDED(hr))
1697 for (j = 0; j < entry_count; ++j)
1699 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
1700 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
1701 if (SUCCEEDED(hr))
1703 expected_refcount = 0;
1704 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
1705 ++expected_refcount;
1706 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
1707 ++expected_refcount;
1708 refcount = IUnknown_Release(iface2);
1709 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
1710 refcount, test_name, i, j, expected_refcount);
1714 expected_refcount = 0;
1715 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
1716 ++expected_refcount;
1717 refcount = IUnknown_Release(iface1);
1718 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
1719 refcount, test_name, i, expected_refcount);
1724 static void test_surface_qi(void)
1726 static const struct qi_test tests[] =
1728 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface, S_OK },
1729 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface, S_OK },
1730 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
1731 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
1732 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
1733 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
1734 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
1735 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
1736 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
1737 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
1738 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
1739 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
1740 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
1741 {&IID_IDirect3D7, NULL, E_INVALIDARG },
1742 {&IID_IDirect3D3, NULL, E_INVALIDARG },
1743 {&IID_IDirect3D2, NULL, E_INVALIDARG },
1744 {&IID_IDirect3D, NULL, E_INVALIDARG },
1745 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
1746 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
1747 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
1748 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
1749 {&IID_IDirectDraw, NULL, E_INVALIDARG },
1750 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
1751 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
1752 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
1753 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
1754 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
1755 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
1756 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
1757 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
1758 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
1759 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
1760 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
1761 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
1762 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
1765 IDirectDrawSurface *surface;
1766 DDSURFACEDESC surface_desc;
1767 IDirect3DDevice *device;
1768 IDirectDraw *ddraw;
1769 HWND window;
1770 HRESULT hr;
1772 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1774 win_skip("DirectDrawCreateEx not available, skipping test.\n");
1775 return;
1778 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1779 0, 0, 640, 480, 0, 0, 0, 0);
1780 ddraw = create_ddraw();
1781 ok(!!ddraw, "Failed to create a ddraw object.\n");
1782 /* Try to create a D3D device to see if the ddraw implementation supports
1783 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
1784 * doesn't support e.g. the IDirect3DTexture interfaces. */
1785 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1787 skip("Failed to create a 3D device, skipping test.\n");
1788 IDirectDraw_Release(ddraw);
1789 DestroyWindow(window);
1790 return;
1792 IDirect3DDevice_Release(device);
1794 memset(&surface_desc, 0, sizeof(surface_desc));
1795 surface_desc.dwSize = sizeof(surface_desc);
1796 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1797 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1798 surface_desc.dwWidth = 512;
1799 surface_desc.dwHeight = 512;
1800 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1801 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1803 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1805 IDirectDrawSurface_Release(surface);
1806 IDirectDraw_Release(ddraw);
1807 DestroyWindow(window);
1810 static void test_device_qi(void)
1812 static const struct qi_test tests[] =
1814 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface, S_OK },
1815 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface, S_OK },
1816 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
1817 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
1818 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
1819 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
1820 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
1821 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
1822 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
1823 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
1824 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
1825 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
1826 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
1827 {&IID_IDirect3DHALDevice, &IID_IDirectDrawSurface, S_OK },
1828 {&IID_IDirect3D7, NULL, E_INVALIDARG },
1829 {&IID_IDirect3D3, NULL, E_INVALIDARG },
1830 {&IID_IDirect3D2, NULL, E_INVALIDARG },
1831 {&IID_IDirect3D, NULL, E_INVALIDARG },
1832 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
1833 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
1834 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
1835 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
1836 {&IID_IDirectDraw, NULL, E_INVALIDARG },
1837 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
1838 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
1839 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
1840 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
1841 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
1842 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
1843 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
1844 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
1845 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
1846 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
1847 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
1848 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
1849 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
1853 IDirect3DDevice *device;
1854 IDirectDraw *ddraw;
1855 HWND window;
1857 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1859 win_skip("DirectDrawCreateEx not available, skipping test.\n");
1860 return;
1863 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1864 0, 0, 640, 480, 0, 0, 0, 0);
1865 ddraw = create_ddraw();
1866 ok(!!ddraw, "Failed to create a ddraw object.\n");
1867 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1869 skip("Failed to create a 3D device, skipping test.\n");
1870 IDirectDraw_Release(ddraw);
1871 DestroyWindow(window);
1872 return;
1875 test_qi("device_qi", (IUnknown *)device, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1877 IDirect3DDevice_Release(device);
1878 IDirectDraw_Release(ddraw);
1879 DestroyWindow(window);
1882 static void test_wndproc(void)
1884 LONG_PTR proc, ddraw_proc;
1885 IDirectDraw *ddraw;
1886 WNDCLASSA wc = {0};
1887 HWND window;
1888 HRESULT hr;
1889 ULONG ref;
1891 static struct message messages[] =
1893 {WM_WINDOWPOSCHANGING, FALSE, 0},
1894 {WM_MOVE, FALSE, 0},
1895 {WM_SIZE, FALSE, 0},
1896 {WM_WINDOWPOSCHANGING, FALSE, 0},
1897 {WM_ACTIVATE, FALSE, 0},
1898 {WM_SETFOCUS, FALSE, 0},
1899 {0, FALSE, 0},
1902 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
1903 ddraw = create_ddraw();
1904 ok(!!ddraw, "Failed to create a ddraw object.\n");
1906 wc.lpfnWndProc = test_proc;
1907 wc.lpszClassName = "ddraw_test_wndproc_wc";
1908 ok(RegisterClassA(&wc), "Failed to register window class.\n");
1910 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
1911 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1913 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1914 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1915 (LONG_PTR)test_proc, proc);
1916 expect_messages = messages;
1917 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1918 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1919 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
1920 expect_messages = NULL;
1921 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1922 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1923 (LONG_PTR)test_proc, proc);
1924 ref = IDirectDraw_Release(ddraw);
1925 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1926 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1927 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1928 (LONG_PTR)test_proc, proc);
1930 /* DDSCL_NORMAL doesn't. */
1931 ddraw = create_ddraw();
1932 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1933 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1934 (LONG_PTR)test_proc, proc);
1935 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
1936 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1937 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1938 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1939 (LONG_PTR)test_proc, proc);
1940 ref = IDirectDraw_Release(ddraw);
1941 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1942 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1943 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1944 (LONG_PTR)test_proc, proc);
1946 /* The original window proc is only restored by ddraw if the current
1947 * window proc matches the one ddraw set. This also affects switching
1948 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
1949 ddraw = create_ddraw();
1950 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1951 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1952 (LONG_PTR)test_proc, proc);
1953 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1954 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
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 ddraw_proc = proc;
1959 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1960 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1961 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1962 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1963 (LONG_PTR)test_proc, proc);
1964 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1965 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1966 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
1967 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1968 (LONG_PTR)test_proc, proc);
1969 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1970 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1971 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1972 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1973 (LONG_PTR)DefWindowProcA, proc);
1974 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1975 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1976 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
1977 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1978 (LONG_PTR)DefWindowProcA, proc);
1979 ref = IDirectDraw_Release(ddraw);
1980 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1981 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1982 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1983 (LONG_PTR)test_proc, proc);
1985 ddraw = create_ddraw();
1986 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1987 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1988 (LONG_PTR)test_proc, proc);
1989 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1990 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
1991 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
1992 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
1993 (LONG_PTR)test_proc, proc);
1994 ref = IDirectDraw_Release(ddraw);
1995 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
1996 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1997 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
1998 (LONG_PTR)DefWindowProcA, proc);
2000 fix_wndproc(window, (LONG_PTR)test_proc);
2001 expect_messages = NULL;
2002 DestroyWindow(window);
2003 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2006 static void test_window_style(void)
2008 LONG style, exstyle, tmp, expected_style;
2009 RECT fullscreen_rect, r;
2010 IDirectDraw *ddraw;
2011 HWND window;
2012 HRESULT hr;
2013 ULONG ref;
2014 BOOL ret;
2016 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2017 0, 0, 100, 100, 0, 0, 0, 0);
2018 ddraw = create_ddraw();
2019 ok(!!ddraw, "Failed to create a ddraw object.\n");
2021 style = GetWindowLongA(window, GWL_STYLE);
2022 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2023 SetRect(&fullscreen_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2025 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2026 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2028 tmp = GetWindowLongA(window, GWL_STYLE);
2029 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2030 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2031 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2033 GetWindowRect(window, &r);
2034 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2035 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2036 r.left, r.top, r.right, r.bottom);
2037 GetClientRect(window, &r);
2038 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2040 ret = SetForegroundWindow(GetDesktopWindow());
2041 ok(ret, "Failed to set foreground window.\n");
2043 tmp = GetWindowLongA(window, GWL_STYLE);
2044 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2045 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2046 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2048 ret = SetForegroundWindow(window);
2049 ok(ret, "Failed to set foreground window.\n");
2050 /* Windows 7 (but not Vista and XP) shows the window when it receives focus. Hide it again,
2051 * the next tests expect this. */
2052 ShowWindow(window, SW_HIDE);
2054 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2055 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2057 tmp = GetWindowLongA(window, GWL_STYLE);
2058 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2059 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2060 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2062 ShowWindow(window, SW_SHOW);
2063 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2064 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2066 tmp = GetWindowLongA(window, GWL_STYLE);
2067 expected_style = style | WS_VISIBLE;
2068 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2069 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2070 expected_style = exstyle | WS_EX_TOPMOST;
2071 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2073 ret = SetForegroundWindow(GetDesktopWindow());
2074 ok(ret, "Failed to set foreground window.\n");
2075 tmp = GetWindowLongA(window, GWL_STYLE);
2076 expected_style = style | WS_VISIBLE | WS_MINIMIZE;
2077 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2078 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2079 expected_style = exstyle | WS_EX_TOPMOST;
2080 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2082 ref = IDirectDraw_Release(ddraw);
2083 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2085 DestroyWindow(window);
2088 static void test_redundant_mode_set(void)
2090 DDSURFACEDESC surface_desc = {0};
2091 IDirectDraw *ddraw;
2092 HWND window;
2093 HRESULT hr;
2094 RECT r, s;
2095 ULONG ref;
2097 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2098 0, 0, 100, 100, 0, 0, 0, 0);
2099 ddraw = create_ddraw();
2100 ok(!!ddraw, "Failed to create a ddraw object.\n");
2102 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2103 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2105 surface_desc.dwSize = sizeof(surface_desc);
2106 hr = IDirectDraw_GetDisplayMode(ddraw, &surface_desc);
2107 ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
2109 hr = IDirectDraw_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2110 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
2111 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2113 GetWindowRect(window, &r);
2114 r.right /= 2;
2115 r.bottom /= 2;
2116 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2117 GetWindowRect(window, &s);
2118 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2119 r.left, r.top, r.right, r.bottom,
2120 s.left, s.top, s.right, s.bottom);
2122 hr = IDirectDraw_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2123 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
2124 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2126 GetWindowRect(window, &s);
2127 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2128 r.left, r.top, r.right, r.bottom,
2129 s.left, s.top, s.right, s.bottom);
2131 ref = IDirectDraw_Release(ddraw);
2132 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2134 DestroyWindow(window);
2137 static SIZE screen_size;
2139 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2141 if (message == WM_SIZE)
2143 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2144 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2147 return test_proc(hwnd, message, wparam, lparam);
2150 struct test_coop_level_mode_set_enum_param
2152 DWORD ddraw_width, ddraw_height, user32_width, user32_height;
2155 static HRESULT CALLBACK test_coop_level_mode_set_enum_cb(DDSURFACEDESC *surface_desc, void *context)
2157 struct test_coop_level_mode_set_enum_param *param = context;
2159 if (U1(surface_desc->ddpfPixelFormat).dwRGBBitCount != registry_mode.dmBitsPerPel)
2160 return DDENUMRET_OK;
2161 if (surface_desc->dwWidth == registry_mode.dmPelsWidth
2162 && surface_desc->dwHeight == registry_mode.dmPelsHeight)
2163 return DDENUMRET_OK;
2165 if (!param->ddraw_width)
2167 param->ddraw_width = surface_desc->dwWidth;
2168 param->ddraw_height = surface_desc->dwHeight;
2169 return DDENUMRET_OK;
2171 if (surface_desc->dwWidth == param->ddraw_width && surface_desc->dwHeight == param->ddraw_height)
2172 return DDENUMRET_OK;
2174 param->user32_width = surface_desc->dwWidth;
2175 param->user32_height = surface_desc->dwHeight;
2176 return DDENUMRET_CANCEL;
2179 static void test_coop_level_mode_set(void)
2181 IDirectDrawSurface *primary;
2182 RECT registry_rect, ddraw_rect, user32_rect, r;
2183 IDirectDraw *ddraw;
2184 DDSURFACEDESC ddsd;
2185 WNDCLASSA wc = {0};
2186 HWND window;
2187 HRESULT hr;
2188 ULONG ref;
2189 MSG msg;
2190 struct test_coop_level_mode_set_enum_param param;
2191 DEVMODEW devmode;
2192 BOOL ret;
2193 LONG change_ret;
2195 static const struct message exclusive_messages[] =
2197 {WM_WINDOWPOSCHANGING, FALSE, 0},
2198 {WM_WINDOWPOSCHANGED, FALSE, 0},
2199 {WM_SIZE, FALSE, 0},
2200 {WM_DISPLAYCHANGE, FALSE, 0},
2201 {0, FALSE, 0},
2203 static const struct message exclusive_focus_loss_messages[] =
2205 {WM_ACTIVATE, TRUE, WA_INACTIVE},
2206 {WM_DISPLAYCHANGE, FALSE, 0},
2207 {WM_WINDOWPOSCHANGING, FALSE, 0},
2208 /* Like d3d8 and d3d9 ddraw seems to use SW_SHOWMINIMIZED instead of
2209 * SW_MINIMIZED, causing a recursive window activation that does not
2210 * produce the same result in Wine yet. Ignore the difference for now.
2211 * {WM_ACTIVATE, TRUE, 0x200000 | WA_ACTIVE}, */
2212 {WM_WINDOWPOSCHANGED, FALSE, 0},
2213 {WM_MOVE, FALSE, 0},
2214 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2215 {WM_ACTIVATEAPP, TRUE, FALSE},
2216 {0, FALSE, 0},
2218 static const struct message exclusive_focus_restore_messages[] =
2220 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* From the ShowWindow(SW_RESTORE). */
2221 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* Generated by ddraw, matches d3d9 behavior. */
2222 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching previous message. */
2223 {WM_SIZE, FALSE, 0}, /* DefWindowProc. */
2224 {WM_DISPLAYCHANGE, FALSE, 0}, /* Ddraw restores mode. */
2225 /* Native redundantly sets the window size here. */
2226 {WM_ACTIVATEAPP, TRUE, TRUE}, /* End of ddraw's hooks. */
2227 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching the one from ShowWindow. */
2228 {WM_MOVE, FALSE, 0}, /* DefWindowProc. */
2229 {WM_SIZE, TRUE, SIZE_RESTORED}, /* DefWindowProc. */
2230 {0, FALSE, 0},
2232 static const struct message sc_restore_messages[] =
2234 {WM_SYSCOMMAND, TRUE, SC_RESTORE},
2235 {WM_WINDOWPOSCHANGING, FALSE, 0},
2236 {WM_WINDOWPOSCHANGED, FALSE, 0},
2237 {WM_SIZE, TRUE, SIZE_RESTORED},
2238 {0, FALSE, 0},
2240 static const struct message sc_minimize_messages[] =
2242 {WM_SYSCOMMAND, TRUE, SC_MINIMIZE},
2243 {WM_WINDOWPOSCHANGING, FALSE, 0},
2244 {WM_WINDOWPOSCHANGED, FALSE, 0},
2245 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2246 {0, FALSE, 0},
2248 static const struct message sc_maximize_messages[] =
2250 {WM_SYSCOMMAND, TRUE, SC_MAXIMIZE},
2251 {WM_WINDOWPOSCHANGING, FALSE, 0},
2252 {WM_WINDOWPOSCHANGED, FALSE, 0},
2253 {WM_SIZE, TRUE, SIZE_MAXIMIZED},
2254 {0, FALSE, 0},
2257 static const struct message normal_messages[] =
2259 {WM_DISPLAYCHANGE, FALSE, 0},
2260 {0, FALSE, 0},
2263 ddraw = create_ddraw();
2264 ok(!!ddraw, "Failed to create a ddraw object.\n");
2266 memset(&param, 0, sizeof(param));
2267 hr = IDirectDraw_EnumDisplayModes(ddraw, 0, NULL, &param, test_coop_level_mode_set_enum_cb);
2268 ok(SUCCEEDED(hr), "Failed to enumerate display mode, hr %#x.\n", hr);
2269 ref = IDirectDraw_Release(ddraw);
2270 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2272 if (!param.user32_height)
2274 skip("Fewer than 3 different modes supported, skipping mode restore test.\n");
2275 return;
2278 SetRect(&registry_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2279 SetRect(&ddraw_rect, 0, 0, param.ddraw_width, param.ddraw_height);
2280 SetRect(&user32_rect, 0, 0, param.user32_width, param.user32_height);
2282 memset(&devmode, 0, sizeof(devmode));
2283 devmode.dmSize = sizeof(devmode);
2284 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2285 devmode.dmPelsWidth = param.user32_width;
2286 devmode.dmPelsHeight = param.user32_height;
2287 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2288 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2290 ddraw = create_ddraw();
2291 ok(!!ddraw, "Failed to create a ddraw object.\n");
2293 wc.lpfnWndProc = mode_set_proc;
2294 wc.lpszClassName = "ddraw_test_wndproc_wc";
2295 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2297 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2298 0, 0, 100, 100, 0, 0, 0, 0);
2300 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2301 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2303 GetWindowRect(window, &r);
2304 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2305 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2306 r.left, r.top, r.right, r.bottom);
2308 memset(&ddsd, 0, sizeof(ddsd));
2309 ddsd.dwSize = sizeof(ddsd);
2310 ddsd.dwFlags = DDSD_CAPS;
2311 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2313 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2314 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2315 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2316 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2317 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2318 param.user32_width, ddsd.dwWidth);
2319 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2320 param.user32_height, ddsd.dwHeight);
2322 GetWindowRect(window, &r);
2323 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2324 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2325 r.left, r.top, r.right, r.bottom);
2327 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2328 expect_messages = exclusive_messages;
2329 screen_size.cx = 0;
2330 screen_size.cy = 0;
2332 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2333 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2335 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2336 expect_messages = NULL;
2337 ok(screen_size.cx == param.ddraw_width && screen_size.cy == param.ddraw_height,
2338 "Expected screen size %ux%u, got %ux%u.\n",
2339 param.ddraw_width, param.ddraw_height, screen_size.cx, screen_size.cy);
2341 GetWindowRect(window, &r);
2342 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2343 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2344 r.left, r.top, r.right, r.bottom);
2346 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2347 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2348 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2349 param.user32_width, ddsd.dwWidth);
2350 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2351 param.user32_height, ddsd.dwHeight);
2352 IDirectDrawSurface_Release(primary);
2354 memset(&ddsd, 0, sizeof(ddsd));
2355 ddsd.dwSize = sizeof(ddsd);
2356 ddsd.dwFlags = DDSD_CAPS;
2357 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2359 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2360 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2361 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2362 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2363 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2364 param.ddraw_width, ddsd.dwWidth);
2365 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2366 param.ddraw_height, ddsd.dwHeight);
2368 GetWindowRect(window, &r);
2369 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2370 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2371 r.left, r.top, r.right, r.bottom);
2373 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2374 expect_messages = exclusive_messages;
2375 screen_size.cx = 0;
2376 screen_size.cy = 0;
2378 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2379 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2381 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2382 expect_messages = NULL;
2383 ok(screen_size.cx == param.user32_width && screen_size.cy == param.user32_height,
2384 "Expected screen size %ux%u, got %ux%u.\n",
2385 param.user32_width, param.user32_height, screen_size.cx, screen_size.cy);
2387 GetWindowRect(window, &r);
2388 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2389 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2390 r.left, r.top, r.right, r.bottom);
2392 expect_messages = exclusive_focus_loss_messages;
2393 ret = SetForegroundWindow(GetDesktopWindow());
2394 ok(ret, "Failed to set foreground window.\n");
2395 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2396 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2397 ok(ret, "Failed to get display mode.\n");
2398 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2399 && devmode.dmPelsHeight == registry_mode.dmPelsHeight, "Got unexpect screen size %ux%u.\n",
2400 devmode.dmPelsWidth, devmode.dmPelsHeight);
2402 expect_messages = exclusive_focus_restore_messages;
2403 ShowWindow(window, SW_RESTORE);
2404 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2406 GetWindowRect(window, &r);
2407 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2408 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2409 r.left, r.top, r.right, r.bottom);
2410 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2411 ok(ret, "Failed to get display mode.\n");
2412 ok(devmode.dmPelsWidth == param.ddraw_width
2413 && devmode.dmPelsHeight == param.ddraw_height, "Got unexpect screen size %ux%u.\n",
2414 devmode.dmPelsWidth, devmode.dmPelsHeight);
2416 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2417 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2418 /* Normally the primary should be restored here. Unfortunately this causes the
2419 * GetSurfaceDesc call after the next display mode change to crash on the Windows 8
2420 * testbot. Another Restore call would presumably avoid the crash, but it also moots
2421 * the point of the GetSurfaceDesc call. */
2423 expect_messages = sc_minimize_messages;
2424 SendMessageA(window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
2425 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2426 expect_messages = NULL;
2428 expect_messages = sc_restore_messages;
2429 SendMessageA(window, WM_SYSCOMMAND, SC_RESTORE, 0);
2430 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2431 expect_messages = NULL;
2433 expect_messages = sc_maximize_messages;
2434 SendMessageA(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
2435 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2436 expect_messages = NULL;
2438 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2439 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2441 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2442 expect_messages = exclusive_messages;
2443 screen_size.cx = 0;
2444 screen_size.cy = 0;
2446 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2447 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2449 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2450 expect_messages = NULL;
2451 ok(screen_size.cx == registry_mode.dmPelsWidth
2452 && screen_size.cy == registry_mode.dmPelsHeight,
2453 "Expected screen size %ux%u, got %ux%u.\n",
2454 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size.cx, screen_size.cy);
2456 GetWindowRect(window, &r);
2457 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2458 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2459 r.left, r.top, r.right, r.bottom);
2461 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2462 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2463 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2464 param.ddraw_width, ddsd.dwWidth);
2465 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2466 param.ddraw_height, ddsd.dwHeight);
2467 IDirectDrawSurface_Release(primary);
2469 /* For Wine. */
2470 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2471 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2473 memset(&ddsd, 0, sizeof(ddsd));
2474 ddsd.dwSize = sizeof(ddsd);
2475 ddsd.dwFlags = DDSD_CAPS;
2476 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2478 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2479 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2480 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2481 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2482 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2483 registry_mode.dmPelsWidth, ddsd.dwWidth);
2484 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2485 registry_mode.dmPelsHeight, ddsd.dwHeight);
2487 GetWindowRect(window, &r);
2488 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2489 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2490 r.left, r.top, r.right, r.bottom);
2492 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2493 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2495 GetWindowRect(window, &r);
2496 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2497 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2498 r.left, r.top, r.right, r.bottom);
2500 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2501 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2502 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2503 registry_mode.dmPelsWidth, ddsd.dwWidth);
2504 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2505 registry_mode.dmPelsHeight, ddsd.dwHeight);
2506 IDirectDrawSurface_Release(primary);
2508 memset(&ddsd, 0, sizeof(ddsd));
2509 ddsd.dwSize = sizeof(ddsd);
2510 ddsd.dwFlags = DDSD_CAPS;
2511 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2513 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2514 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2515 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2516 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2517 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2518 registry_mode.dmPelsWidth, ddsd.dwWidth);
2519 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2520 registry_mode.dmPelsHeight, ddsd.dwHeight);
2522 GetWindowRect(window, &r);
2523 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2524 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2525 r.left, r.top, r.right, r.bottom);
2527 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2528 expect_messages = normal_messages;
2529 screen_size.cx = 0;
2530 screen_size.cy = 0;
2532 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2533 devmode.dmPelsWidth = param.user32_width;
2534 devmode.dmPelsHeight = param.user32_height;
2535 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2536 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2538 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2539 expect_messages = NULL;
2540 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2542 GetWindowRect(window, &r);
2543 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2544 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2545 r.left, r.top, r.right, r.bottom);
2547 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2548 expect_messages = normal_messages;
2549 screen_size.cx = 0;
2550 screen_size.cy = 0;
2552 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2553 if (hr == DDERR_NOEXCLUSIVEMODE /* NT4 testbot */)
2555 win_skip("Broken SetDisplayMode(), skipping remaining tests.\n");
2556 IDirectDrawSurface_Release(primary);
2557 IDirectDraw_Release(ddraw);
2558 goto done;
2560 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2562 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2563 expect_messages = NULL;
2564 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2566 GetWindowRect(window, &r);
2567 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2568 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2569 r.left, r.top, r.right, r.bottom);
2571 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2572 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2573 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2574 registry_mode.dmPelsWidth, ddsd.dwWidth);
2575 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2576 registry_mode.dmPelsHeight, ddsd.dwHeight);
2577 IDirectDrawSurface_Release(primary);
2579 memset(&ddsd, 0, sizeof(ddsd));
2580 ddsd.dwSize = sizeof(ddsd);
2581 ddsd.dwFlags = DDSD_CAPS;
2582 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2584 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2585 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2586 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2587 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2588 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2589 param.ddraw_width, ddsd.dwWidth);
2590 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2591 param.ddraw_height, ddsd.dwHeight);
2593 GetWindowRect(window, &r);
2594 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2595 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2596 r.left, r.top, r.right, r.bottom);
2598 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2599 expect_messages = normal_messages;
2600 screen_size.cx = 0;
2601 screen_size.cy = 0;
2603 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2604 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2606 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2607 expect_messages = NULL;
2608 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2610 GetWindowRect(window, &r);
2611 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2612 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2613 r.left, r.top, r.right, r.bottom);
2615 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2616 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2617 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2618 param.ddraw_width, ddsd.dwWidth);
2619 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2620 param.ddraw_height, ddsd.dwHeight);
2621 IDirectDrawSurface_Release(primary);
2623 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2624 ok(ret, "Failed to get display mode.\n");
2625 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2626 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
2627 "Expected resolution %ux%u, got %ux%u.\n",
2628 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
2629 devmode.dmPelsWidth, devmode.dmPelsHeight);
2630 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2631 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2633 memset(&ddsd, 0, sizeof(ddsd));
2634 ddsd.dwSize = sizeof(ddsd);
2635 ddsd.dwFlags = DDSD_CAPS;
2636 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2638 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2639 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2640 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2641 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2642 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2643 registry_mode.dmPelsWidth, ddsd.dwWidth);
2644 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2645 registry_mode.dmPelsHeight, ddsd.dwHeight);
2647 GetWindowRect(window, &r);
2648 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2649 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2650 r.left, r.top, r.right, r.bottom);
2652 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
2653 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
2654 * not DDSCL_FULLSCREEN. */
2655 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2656 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2658 GetWindowRect(window, &r);
2659 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2660 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2661 r.left, r.top, r.right, r.bottom);
2663 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2664 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2665 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2666 registry_mode.dmPelsWidth, ddsd.dwWidth);
2667 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2668 registry_mode.dmPelsHeight, ddsd.dwHeight);
2669 IDirectDrawSurface_Release(primary);
2671 memset(&ddsd, 0, sizeof(ddsd));
2672 ddsd.dwSize = sizeof(ddsd);
2673 ddsd.dwFlags = DDSD_CAPS;
2674 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2676 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2677 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2678 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2679 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2680 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2681 registry_mode.dmPelsWidth, ddsd.dwWidth);
2682 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2683 registry_mode.dmPelsHeight, ddsd.dwHeight);
2685 GetWindowRect(window, &r);
2686 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2687 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2688 r.left, r.top, r.right, r.bottom);
2690 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2691 expect_messages = normal_messages;
2692 screen_size.cx = 0;
2693 screen_size.cy = 0;
2695 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2696 devmode.dmPelsWidth = param.user32_width;
2697 devmode.dmPelsHeight = param.user32_height;
2698 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2699 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2701 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2702 expect_messages = NULL;
2703 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2705 GetWindowRect(window, &r);
2706 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2707 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2708 r.left, r.top, r.right, r.bottom);
2710 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2711 expect_messages = normal_messages;
2712 screen_size.cx = 0;
2713 screen_size.cy = 0;
2715 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2716 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2718 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2719 expect_messages = NULL;
2720 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2722 GetWindowRect(window, &r);
2723 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2724 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2725 r.left, r.top, r.right, r.bottom);
2727 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2728 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2729 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2730 registry_mode.dmPelsWidth, ddsd.dwWidth);
2731 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2732 registry_mode.dmPelsHeight, ddsd.dwHeight);
2733 IDirectDrawSurface_Release(primary);
2735 memset(&ddsd, 0, sizeof(ddsd));
2736 ddsd.dwSize = sizeof(ddsd);
2737 ddsd.dwFlags = DDSD_CAPS;
2738 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2740 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2741 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2742 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2743 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2744 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2745 param.ddraw_width, ddsd.dwWidth);
2746 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2747 param.ddraw_height, ddsd.dwHeight);
2749 GetWindowRect(window, &r);
2750 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2751 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2752 r.left, r.top, r.right, r.bottom);
2754 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2755 expect_messages = normal_messages;
2756 screen_size.cx = 0;
2757 screen_size.cy = 0;
2759 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2760 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2762 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2763 expect_messages = NULL;
2764 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2766 GetWindowRect(window, &r);
2767 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2768 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2769 r.left, r.top, r.right, r.bottom);
2771 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2772 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2773 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2774 param.ddraw_width, ddsd.dwWidth);
2775 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2776 param.ddraw_height, ddsd.dwHeight);
2777 IDirectDrawSurface_Release(primary);
2779 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2780 ok(ret, "Failed to get display mode.\n");
2781 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2782 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
2783 "Expected resolution %ux%u, got %ux%u.\n",
2784 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
2785 devmode.dmPelsWidth, devmode.dmPelsHeight);
2786 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2787 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2789 memset(&ddsd, 0, sizeof(ddsd));
2790 ddsd.dwSize = sizeof(ddsd);
2791 ddsd.dwFlags = DDSD_CAPS;
2792 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2794 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2795 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2796 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2797 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2798 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2799 registry_mode.dmPelsWidth, ddsd.dwWidth);
2800 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2801 registry_mode.dmPelsHeight, ddsd.dwHeight);
2802 IDirectDrawSurface_Release(primary);
2804 GetWindowRect(window, &r);
2805 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2806 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2807 r.left, r.top, r.right, r.bottom);
2809 /* Unlike ddraw2-7, changing from EXCLUSIVE to NORMAL does not restore the resolution */
2810 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2811 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2812 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2813 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2815 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2816 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2818 memset(&ddsd, 0, sizeof(ddsd));
2819 ddsd.dwSize = sizeof(ddsd);
2820 ddsd.dwFlags = DDSD_CAPS;
2821 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2823 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2824 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2825 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2826 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2827 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2828 param.ddraw_width, ddsd.dwWidth);
2829 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2830 param.ddraw_height, ddsd.dwHeight);
2831 IDirectDrawSurface_Release(primary);
2832 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2833 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2835 ref = IDirectDraw_Release(ddraw);
2836 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2838 GetWindowRect(window, &r);
2839 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2840 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2841 r.left, r.top, r.right, r.bottom);
2843 done:
2844 expect_messages = NULL;
2845 DestroyWindow(window);
2846 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2849 static void test_coop_level_mode_set_multi(void)
2851 IDirectDraw *ddraw1, *ddraw2;
2852 UINT w, h;
2853 HWND window;
2854 HRESULT hr;
2855 ULONG ref;
2857 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2858 0, 0, 100, 100, 0, 0, 0, 0);
2859 ddraw1 = create_ddraw();
2860 ok(!!ddraw1, "Failed to create a ddraw object.\n");
2862 /* With just a single ddraw object, the display mode is restored on
2863 * release. */
2864 hr = set_display_mode(ddraw1, 800, 600);
2865 if (hr == DDERR_NOEXCLUSIVEMODE /* NT4 testbot */)
2867 win_skip("Broken SetDisplayMode(), skipping test.\n");
2868 IDirectDraw_Release(ddraw1);
2869 DestroyWindow(window);
2870 return;
2872 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2873 w = GetSystemMetrics(SM_CXSCREEN);
2874 ok(w == 800, "Got unexpected screen width %u.\n", w);
2875 h = GetSystemMetrics(SM_CYSCREEN);
2876 ok(h == 600, "Got unexpected screen height %u.\n", h);
2878 ref = IDirectDraw_Release(ddraw1);
2879 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2880 w = GetSystemMetrics(SM_CXSCREEN);
2881 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
2882 h = GetSystemMetrics(SM_CYSCREEN);
2883 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
2885 /* When there are multiple ddraw objects, the display mode is restored to
2886 * the initial mode, before the first SetDisplayMode() call. */
2887 ddraw1 = create_ddraw();
2888 hr = set_display_mode(ddraw1, 800, 600);
2889 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2890 w = GetSystemMetrics(SM_CXSCREEN);
2891 ok(w == 800, "Got unexpected screen width %u.\n", w);
2892 h = GetSystemMetrics(SM_CYSCREEN);
2893 ok(h == 600, "Got unexpected screen height %u.\n", h);
2895 ddraw2 = create_ddraw();
2896 hr = set_display_mode(ddraw2, 640, 480);
2897 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2898 w = GetSystemMetrics(SM_CXSCREEN);
2899 ok(w == 640, "Got unexpected screen width %u.\n", w);
2900 h = GetSystemMetrics(SM_CYSCREEN);
2901 ok(h == 480, "Got unexpected screen height %u.\n", h);
2903 ref = IDirectDraw_Release(ddraw2);
2904 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2905 w = GetSystemMetrics(SM_CXSCREEN);
2906 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
2907 h = GetSystemMetrics(SM_CYSCREEN);
2908 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
2910 ref = IDirectDraw_Release(ddraw1);
2911 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2912 w = GetSystemMetrics(SM_CXSCREEN);
2913 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
2914 h = GetSystemMetrics(SM_CYSCREEN);
2915 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
2917 /* Regardless of release ordering. */
2918 ddraw1 = create_ddraw();
2919 hr = set_display_mode(ddraw1, 800, 600);
2920 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2921 w = GetSystemMetrics(SM_CXSCREEN);
2922 ok(w == 800, "Got unexpected screen width %u.\n", w);
2923 h = GetSystemMetrics(SM_CYSCREEN);
2924 ok(h == 600, "Got unexpected screen height %u.\n", h);
2926 ddraw2 = create_ddraw();
2927 hr = set_display_mode(ddraw2, 640, 480);
2928 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2929 w = GetSystemMetrics(SM_CXSCREEN);
2930 ok(w == 640, "Got unexpected screen width %u.\n", w);
2931 h = GetSystemMetrics(SM_CYSCREEN);
2932 ok(h == 480, "Got unexpected screen height %u.\n", h);
2934 ref = IDirectDraw_Release(ddraw1);
2935 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2936 w = GetSystemMetrics(SM_CXSCREEN);
2937 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
2938 h = GetSystemMetrics(SM_CYSCREEN);
2939 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
2941 ref = IDirectDraw_Release(ddraw2);
2942 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2943 w = GetSystemMetrics(SM_CXSCREEN);
2944 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
2945 h = GetSystemMetrics(SM_CYSCREEN);
2946 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
2948 /* But only for ddraw objects that called SetDisplayMode(). */
2949 ddraw1 = create_ddraw();
2950 ddraw2 = create_ddraw();
2951 hr = set_display_mode(ddraw2, 640, 480);
2952 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2953 w = GetSystemMetrics(SM_CXSCREEN);
2954 ok(w == 640, "Got unexpected screen width %u.\n", w);
2955 h = GetSystemMetrics(SM_CYSCREEN);
2956 ok(h == 480, "Got unexpected screen height %u.\n", h);
2958 ref = IDirectDraw_Release(ddraw1);
2959 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2960 w = GetSystemMetrics(SM_CXSCREEN);
2961 ok(w == 640, "Got unexpected screen width %u.\n", w);
2962 h = GetSystemMetrics(SM_CYSCREEN);
2963 ok(h == 480, "Got unexpected screen height %u.\n", h);
2965 ref = IDirectDraw_Release(ddraw2);
2966 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2967 w = GetSystemMetrics(SM_CXSCREEN);
2968 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
2969 h = GetSystemMetrics(SM_CYSCREEN);
2970 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
2972 /* If there's a ddraw object that's currently in exclusive mode, it blocks
2973 * restoring the display mode. */
2974 ddraw1 = create_ddraw();
2975 hr = set_display_mode(ddraw1, 800, 600);
2976 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2977 w = GetSystemMetrics(SM_CXSCREEN);
2978 ok(w == 800, "Got unexpected screen width %u.\n", w);
2979 h = GetSystemMetrics(SM_CYSCREEN);
2980 ok(h == 600, "Got unexpected screen height %u.\n", h);
2982 ddraw2 = create_ddraw();
2983 hr = set_display_mode(ddraw2, 640, 480);
2984 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2985 w = GetSystemMetrics(SM_CXSCREEN);
2986 ok(w == 640, "Got unexpected screen width %u.\n", w);
2987 h = GetSystemMetrics(SM_CYSCREEN);
2988 ok(h == 480, "Got unexpected screen height %u.\n", h);
2990 hr = IDirectDraw_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2991 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2993 ref = IDirectDraw_Release(ddraw1);
2994 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2995 w = GetSystemMetrics(SM_CXSCREEN);
2996 ok(w == 640, "Got unexpected screen width %u.\n", w);
2997 h = GetSystemMetrics(SM_CYSCREEN);
2998 ok(h == 480, "Got unexpected screen height %u.\n", h);
3000 ref = IDirectDraw_Release(ddraw2);
3001 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3002 w = GetSystemMetrics(SM_CXSCREEN);
3003 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3004 h = GetSystemMetrics(SM_CYSCREEN);
3005 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3007 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
3008 ddraw1 = create_ddraw();
3009 hr = set_display_mode(ddraw1, 800, 600);
3010 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3011 w = GetSystemMetrics(SM_CXSCREEN);
3012 ok(w == 800, "Got unexpected screen width %u.\n", w);
3013 h = GetSystemMetrics(SM_CYSCREEN);
3014 ok(h == 600, "Got unexpected screen height %u.\n", h);
3016 hr = IDirectDraw_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3017 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3019 ddraw2 = create_ddraw();
3020 hr = set_display_mode(ddraw2, 640, 480);
3021 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
3023 ref = IDirectDraw_Release(ddraw1);
3024 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3025 w = GetSystemMetrics(SM_CXSCREEN);
3026 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3027 h = GetSystemMetrics(SM_CYSCREEN);
3028 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3030 ref = IDirectDraw_Release(ddraw2);
3031 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3032 w = GetSystemMetrics(SM_CXSCREEN);
3033 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3034 h = GetSystemMetrics(SM_CYSCREEN);
3035 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3037 DestroyWindow(window);
3040 static void test_initialize(void)
3042 IDirectDraw *ddraw;
3043 IDirect3D *d3d;
3044 HRESULT hr;
3046 ddraw = create_ddraw();
3047 ok(!!ddraw, "Failed to create a ddraw object.\n");
3049 hr = IDirectDraw_Initialize(ddraw, NULL);
3050 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
3051 IDirectDraw_Release(ddraw);
3053 CoInitialize(NULL);
3054 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw, (void **)&ddraw);
3055 ok(SUCCEEDED(hr), "Failed to create IDirectDraw instance, hr %#x.\n", hr);
3056 hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
3057 if (SUCCEEDED(hr))
3059 /* IDirect3D_Initialize() just returns DDERR_ALREADYINITIALIZED. */
3060 hr = IDirect3D_Initialize(d3d, NULL);
3061 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3062 IDirect3D_Release(d3d);
3064 else
3065 skip("D3D interface is not available, skipping test.\n");
3066 hr = IDirectDraw_Initialize(ddraw, NULL);
3067 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
3068 hr = IDirectDraw_Initialize(ddraw, NULL);
3069 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3070 IDirectDraw_Release(ddraw);
3071 CoUninitialize();
3073 if (0) /* This crashes on the W2KPROSP4 testbot. */
3075 CoInitialize(NULL);
3076 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirect3D, (void **)&d3d);
3077 ok(hr == E_NOINTERFACE, "CoCreateInstance returned hr %#x, expected E_NOINTERFACE.\n", hr);
3078 CoUninitialize();
3082 static void test_coop_level_surf_create(void)
3084 IDirectDrawSurface *surface;
3085 IDirectDraw *ddraw;
3086 DDSURFACEDESC ddsd;
3087 HRESULT hr;
3089 ddraw = create_ddraw();
3090 ok(!!ddraw, "Failed to create a ddraw object.\n");
3092 memset(&ddsd, 0, sizeof(ddsd));
3093 ddsd.dwSize = sizeof(ddsd);
3094 ddsd.dwFlags = DDSD_CAPS;
3095 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3096 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3097 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3099 IDirectDraw_Release(ddraw);
3102 static void test_coop_level_multi_window(void)
3104 HWND window1, window2;
3105 IDirectDraw *ddraw;
3106 HRESULT hr;
3108 window1 = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3109 0, 0, 640, 480, 0, 0, 0, 0);
3110 window2 = CreateWindowA("static", "ddraw_test2", WS_OVERLAPPEDWINDOW,
3111 0, 0, 640, 480, 0, 0, 0, 0);
3112 ddraw = create_ddraw();
3113 ok(!!ddraw, "Failed to create a ddraw object.\n");
3115 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3116 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3117 hr = IDirectDraw_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3118 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3119 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3120 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3122 IDirectDraw_Release(ddraw);
3123 DestroyWindow(window2);
3124 DestroyWindow(window1);
3127 static void test_clear_rect_count(void)
3129 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3130 IDirect3DMaterial *white, *red, *green, *blue;
3131 IDirect3DViewport *viewport;
3132 IDirect3DDevice *device;
3133 IDirectDrawSurface *rt;
3134 IDirectDraw *ddraw;
3135 D3DCOLOR color;
3136 HWND window;
3137 HRESULT hr;
3139 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3140 0, 0, 640, 480, 0, 0, 0, 0);
3141 ddraw = create_ddraw();
3142 ok(!!ddraw, "Failed to create a ddraw object.\n");
3143 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3145 skip("Failed to create a 3D device, skipping test.\n");
3146 IDirectDraw_Release(ddraw);
3147 DestroyWindow(window);
3148 return;
3151 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
3152 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3154 white = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
3155 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
3156 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
3157 blue = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
3158 viewport = create_viewport(device, 0, 0, 640, 480);
3160 viewport_set_background(device, viewport, white);
3161 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
3162 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3163 viewport_set_background(device, viewport, red);
3164 hr = IDirect3DViewport_Clear(viewport, 0, &clear_rect, D3DCLEAR_TARGET);
3165 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3166 viewport_set_background(device, viewport, green);
3167 hr = IDirect3DViewport_Clear(viewport, 0, NULL, D3DCLEAR_TARGET);
3168 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3169 viewport_set_background(device, viewport, blue);
3170 hr = IDirect3DViewport_Clear(viewport, 1, NULL, D3DCLEAR_TARGET);
3171 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3173 color = get_surface_color(rt, 320, 240);
3174 ok(compare_color(color, 0x00ffffff, 1), "Got unexpected color 0x%08x.\n", color);
3176 IDirectDrawSurface_Release(rt);
3177 destroy_viewport(device, viewport);
3178 destroy_material(white);
3179 destroy_material(red);
3180 destroy_material(green);
3181 destroy_material(blue);
3182 IDirect3DDevice_Release(device);
3183 IDirectDraw_Release(ddraw);
3184 DestroyWindow(window);
3187 static struct
3189 BOOL received;
3190 IDirectDraw *ddraw;
3191 HWND window;
3192 DWORD coop_level;
3193 } activateapp_testdata;
3195 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
3197 if (message == WM_ACTIVATEAPP)
3199 if (activateapp_testdata.ddraw)
3201 HRESULT hr;
3202 activateapp_testdata.received = FALSE;
3203 hr = IDirectDraw_SetCooperativeLevel(activateapp_testdata.ddraw,
3204 activateapp_testdata.window, activateapp_testdata.coop_level);
3205 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
3206 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
3208 activateapp_testdata.received = TRUE;
3211 return DefWindowProcA(hwnd, message, wparam, lparam);
3214 static void test_coop_level_activateapp(void)
3216 IDirectDraw *ddraw;
3217 HRESULT hr;
3218 HWND window;
3219 WNDCLASSA wc = {0};
3220 DDSURFACEDESC ddsd;
3221 IDirectDrawSurface *surface;
3223 ddraw = create_ddraw();
3224 ok(!!ddraw, "Failed to create a ddraw object.\n");
3226 wc.lpfnWndProc = activateapp_test_proc;
3227 wc.lpszClassName = "ddraw_test_wndproc_wc";
3228 ok(RegisterClassA(&wc), "Failed to register window class.\n");
3230 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
3231 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
3233 /* Exclusive with window already active. */
3234 SetForegroundWindow(window);
3235 activateapp_testdata.received = FALSE;
3236 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3237 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3238 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
3239 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3240 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3242 /* Exclusive with window not active. */
3243 SetForegroundWindow(GetDesktopWindow());
3244 activateapp_testdata.received = FALSE;
3245 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3246 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3247 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3248 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3249 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3251 /* Normal with window not active, then exclusive with the same window. */
3252 SetForegroundWindow(GetDesktopWindow());
3253 activateapp_testdata.received = FALSE;
3254 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3255 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3256 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
3257 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3258 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3259 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3260 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3261 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3263 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
3264 SetForegroundWindow(GetDesktopWindow());
3265 activateapp_testdata.received = FALSE;
3266 activateapp_testdata.ddraw = ddraw;
3267 activateapp_testdata.window = window;
3268 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
3269 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3270 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3271 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3272 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3273 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3275 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
3276 * succeeding. Another switch to exclusive and back to normal is needed to release the
3277 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
3278 * WM_ACTIVATEAPP messages. */
3279 activateapp_testdata.ddraw = NULL;
3280 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3281 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3282 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3283 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3285 /* Setting DDSCL_NORMAL with recursive invocation. */
3286 SetForegroundWindow(GetDesktopWindow());
3287 activateapp_testdata.received = FALSE;
3288 activateapp_testdata.ddraw = ddraw;
3289 activateapp_testdata.window = window;
3290 activateapp_testdata.coop_level = DDSCL_NORMAL;
3291 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3292 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3293 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3295 /* DDraw is in exlusive mode now. */
3296 memset(&ddsd, 0, sizeof(ddsd));
3297 ddsd.dwSize = sizeof(ddsd);
3298 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
3299 ddsd.dwBackBufferCount = 1;
3300 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
3301 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3302 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
3303 IDirectDrawSurface_Release(surface);
3305 /* Recover again, just to be sure. */
3306 activateapp_testdata.ddraw = NULL;
3307 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3308 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3309 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3310 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3312 DestroyWindow(window);
3313 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3314 IDirectDraw_Release(ddraw);
3317 struct format_support_check
3319 const DDPIXELFORMAT *format;
3320 BOOL supported;
3323 static HRESULT WINAPI test_unsupported_formats_cb(DDSURFACEDESC *desc, void *ctx)
3325 struct format_support_check *format = ctx;
3327 if (!memcmp(format->format, &desc->ddpfPixelFormat, sizeof(*format->format)))
3329 format->supported = TRUE;
3330 return DDENUMRET_CANCEL;
3333 return DDENUMRET_OK;
3336 static void test_unsupported_formats(void)
3338 HRESULT hr;
3339 BOOL expect_success;
3340 HWND window;
3341 IDirectDraw *ddraw;
3342 IDirect3DDevice *device;
3343 IDirectDrawSurface *surface;
3344 DDSURFACEDESC ddsd;
3345 unsigned int i, j;
3346 DWORD expected_caps;
3347 static const struct
3349 const char *name;
3350 DDPIXELFORMAT fmt;
3352 formats[] =
3355 "D3DFMT_A8R8G8B8",
3357 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3358 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
3362 "D3DFMT_P8",
3364 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
3365 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3369 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
3371 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3372 0, 0, 640, 480, 0, 0, 0, 0);
3373 ddraw = create_ddraw();
3374 ok(!!ddraw, "Failed to create a ddraw object.\n");
3375 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3377 skip("Failed to create a 3D device, skipping test.\n");
3378 IDirectDraw_Release(ddraw);
3379 DestroyWindow(window);
3380 return;
3383 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
3385 struct format_support_check check = {&formats[i].fmt, FALSE};
3386 hr = IDirect3DDevice_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
3387 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
3389 for (j = 0; j < sizeof(caps) / sizeof(*caps); j++)
3391 memset(&ddsd, 0, sizeof(ddsd));
3392 ddsd.dwSize = sizeof(ddsd);
3393 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
3394 ddsd.ddpfPixelFormat = formats[i].fmt;
3395 ddsd.dwWidth = 4;
3396 ddsd.dwHeight = 4;
3397 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
3399 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
3400 expect_success = FALSE;
3401 else
3402 expect_success = TRUE;
3404 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3405 ok(SUCCEEDED(hr) == expect_success,
3406 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
3407 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
3408 if (FAILED(hr))
3409 continue;
3411 memset(&ddsd, 0, sizeof(ddsd));
3412 ddsd.dwSize = sizeof(ddsd);
3413 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
3414 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3416 if (caps[j] & DDSCAPS_VIDEOMEMORY)
3417 expected_caps = DDSCAPS_VIDEOMEMORY;
3418 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
3419 expected_caps = DDSCAPS_SYSTEMMEMORY;
3420 else if (check.supported)
3421 expected_caps = DDSCAPS_VIDEOMEMORY;
3422 else
3423 expected_caps = DDSCAPS_SYSTEMMEMORY;
3425 ok(ddsd.ddsCaps.dwCaps & expected_caps,
3426 "Expected capability %#x, format %s, input cap %#x.\n",
3427 expected_caps, formats[i].name, caps[j]);
3429 IDirectDrawSurface_Release(surface);
3433 IDirect3DDevice_Release(device);
3434 IDirectDraw_Release(ddraw);
3435 DestroyWindow(window);
3438 static void test_rt_caps(void)
3440 PALETTEENTRY palette_entries[256];
3441 IDirectDrawPalette *palette;
3442 IDirect3DDevice *device;
3443 IDirectDraw *ddraw;
3444 DWORD z_depth = 0;
3445 unsigned int i;
3446 ULONG refcount;
3447 HWND window;
3448 HRESULT hr;
3450 static const DDPIXELFORMAT p8_fmt =
3452 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
3453 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
3456 static const struct
3458 const DDPIXELFORMAT *pf;
3459 DWORD caps_in;
3460 DWORD caps_out;
3461 HRESULT create_device_hr;
3462 BOOL create_may_fail;
3464 test_data[] =
3467 NULL,
3468 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
3469 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3470 D3D_OK,
3471 FALSE,
3474 NULL,
3475 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
3476 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3477 D3D_OK,
3478 FALSE,
3481 NULL,
3482 DDSCAPS_OFFSCREENPLAIN,
3483 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3484 DDERR_INVALIDCAPS,
3485 FALSE,
3488 NULL,
3489 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3490 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3491 D3DERR_SURFACENOTINVIDMEM,
3492 FALSE,
3495 NULL,
3496 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3497 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3498 DDERR_INVALIDCAPS,
3499 FALSE,
3502 NULL,
3503 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
3504 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3505 D3D_OK,
3506 FALSE,
3509 NULL,
3510 DDSCAPS_3DDEVICE,
3511 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3512 D3D_OK,
3513 FALSE,
3516 NULL,
3518 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3519 DDERR_INVALIDCAPS,
3520 FALSE,
3523 NULL,
3524 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3525 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3526 D3DERR_SURFACENOTINVIDMEM,
3527 FALSE,
3530 NULL,
3531 DDSCAPS_SYSTEMMEMORY,
3532 DDSCAPS_SYSTEMMEMORY,
3533 DDERR_INVALIDCAPS,
3534 FALSE,
3537 &p8_fmt,
3539 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3540 DDERR_INVALIDCAPS,
3541 FALSE,
3544 &p8_fmt,
3545 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
3546 ~0U /* AMD r200 */ ,
3547 DDERR_NOPALETTEATTACHED,
3548 FALSE,
3551 &p8_fmt,
3552 DDSCAPS_OFFSCREENPLAIN,
3553 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3554 DDERR_INVALIDCAPS,
3555 FALSE,
3558 &p8_fmt,
3559 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3560 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3561 DDERR_NOPALETTEATTACHED,
3562 FALSE,
3565 &p8_fmt,
3566 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3567 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3568 DDERR_INVALIDCAPS,
3569 FALSE,
3572 NULL,
3573 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
3574 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
3575 DDERR_INVALIDCAPS,
3576 TRUE /* AMD Evergreen */,
3579 NULL,
3580 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
3581 ~0U /* AMD Evergreen */,
3582 DDERR_INVALIDCAPS,
3583 FALSE,
3586 NULL,
3587 DDSCAPS_ZBUFFER,
3588 ~0U /* AMD Evergreen */,
3589 DDERR_INVALIDCAPS,
3590 FALSE,
3593 NULL,
3594 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
3595 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
3596 DDERR_INVALIDCAPS,
3597 TRUE /* Nvidia Kepler */,
3600 NULL,
3601 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
3602 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
3603 DDERR_INVALIDCAPS,
3604 TRUE /* Nvidia Kepler */,
3608 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3609 0, 0, 640, 480, 0, 0, 0, 0);
3610 ddraw = create_ddraw();
3611 ok(!!ddraw, "Failed to create a ddraw object.\n");
3612 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3614 skip("Failed to create a 3D device, skipping test.\n");
3615 IDirectDraw_Release(ddraw);
3616 DestroyWindow(window);
3617 return;
3619 z_depth = get_device_z_depth(device);
3620 ok(!!z_depth, "Failed to get device z depth.\n");
3621 IDirect3DDevice_Release(device);
3623 memset(palette_entries, 0, sizeof(palette_entries));
3624 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
3625 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
3627 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
3629 IDirectDrawSurface *surface;
3630 DDSURFACEDESC surface_desc;
3631 IDirect3DDevice *device;
3633 memset(&surface_desc, 0, sizeof(surface_desc));
3634 surface_desc.dwSize = sizeof(surface_desc);
3635 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3636 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
3637 if (test_data[i].pf)
3639 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
3640 surface_desc.ddpfPixelFormat = *test_data[i].pf;
3642 if (test_data[i].caps_in & DDSCAPS_ZBUFFER)
3644 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
3645 U2(surface_desc).dwZBufferBitDepth = z_depth;
3647 surface_desc.dwWidth = 640;
3648 surface_desc.dwHeight = 480;
3649 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
3650 ok(SUCCEEDED(hr) || broken(test_data[i].create_may_fail),
3651 "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
3652 i, test_data[i].caps_in, hr);
3653 if (FAILED(hr))
3654 continue;
3656 memset(&surface_desc, 0, sizeof(surface_desc));
3657 surface_desc.dwSize = sizeof(surface_desc);
3658 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
3659 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
3660 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
3661 "Test %u: Got unexpected caps %#x, expected %#x.\n",
3662 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
3664 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
3665 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
3666 i, hr, test_data[i].create_device_hr);
3667 if (hr == DDERR_NOPALETTEATTACHED)
3669 hr = IDirectDrawSurface_SetPalette(surface, palette);
3670 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
3671 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
3672 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
3673 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
3674 else
3675 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
3677 if (SUCCEEDED(hr))
3679 refcount = IDirect3DDevice_Release(device);
3680 ok(refcount == 1, "Test %u: Got unexpected refcount %u.\n", i, refcount);
3683 refcount = IDirectDrawSurface_Release(surface);
3684 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
3687 IDirectDrawPalette_Release(palette);
3688 refcount = IDirectDraw_Release(ddraw);
3689 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
3690 DestroyWindow(window);
3693 static void test_primary_caps(void)
3695 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
3696 IDirectDrawSurface *surface;
3697 DDSURFACEDESC surface_desc;
3698 IDirectDraw *ddraw;
3699 unsigned int i;
3700 ULONG refcount;
3701 HWND window;
3702 HRESULT hr;
3704 static const struct
3706 DWORD coop_level;
3707 DWORD caps_in;
3708 DWORD back_buffer_count;
3709 HRESULT hr;
3710 DWORD caps_out;
3712 test_data[] =
3715 DDSCL_NORMAL,
3716 DDSCAPS_PRIMARYSURFACE,
3717 ~0u,
3718 DD_OK,
3719 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
3722 DDSCL_NORMAL,
3723 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
3724 ~0u,
3725 DDERR_INVALIDCAPS,
3726 ~0u,
3729 DDSCL_NORMAL,
3730 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
3731 ~0u,
3732 DD_OK,
3733 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
3736 DDSCL_NORMAL,
3737 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
3738 ~0u,
3739 DDERR_INVALIDCAPS,
3740 ~0u,
3743 DDSCL_NORMAL,
3744 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
3745 ~0u,
3746 DDERR_INVALIDCAPS,
3747 ~0u,
3750 DDSCL_NORMAL,
3751 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
3752 ~0u,
3753 DDERR_INVALIDCAPS,
3754 ~0u,
3757 DDSCL_NORMAL,
3758 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3759 ~0u,
3760 DDERR_INVALIDCAPS,
3761 ~0u,
3764 DDSCL_NORMAL,
3765 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3767 DDERR_INVALIDCAPS,
3768 ~0u,
3771 DDSCL_NORMAL,
3772 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3774 DDERR_NOEXCLUSIVEMODE,
3775 ~0u,
3778 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3779 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3781 DDERR_INVALIDCAPS,
3782 ~0u,
3785 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3786 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3788 DD_OK,
3789 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
3792 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3793 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
3795 DDERR_INVALIDCAPS,
3796 ~0u,
3799 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3800 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
3802 DDERR_INVALIDCAPS,
3803 ~0u,
3807 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3808 0, 0, 640, 480, 0, 0, 0, 0);
3809 ddraw = create_ddraw();
3810 ok(!!ddraw, "Failed to create a ddraw object.\n");
3812 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
3814 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
3815 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3817 memset(&surface_desc, 0, sizeof(surface_desc));
3818 surface_desc.dwSize = sizeof(surface_desc);
3819 surface_desc.dwFlags = DDSD_CAPS;
3820 if (test_data[i].back_buffer_count != ~0u)
3821 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
3822 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
3823 surface_desc.dwBackBufferCount = test_data[i].back_buffer_count;
3824 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
3825 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
3826 if (FAILED(hr))
3827 continue;
3829 memset(&surface_desc, 0, sizeof(surface_desc));
3830 surface_desc.dwSize = sizeof(surface_desc);
3831 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
3832 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
3833 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
3834 "Test %u: Got unexpected caps %#x, expected %#x.\n",
3835 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
3837 IDirectDrawSurface_Release(surface);
3840 refcount = IDirectDraw_Release(ddraw);
3841 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
3842 DestroyWindow(window);
3845 static void test_surface_lock(void)
3847 IDirectDraw *ddraw;
3848 IDirectDrawSurface *surface;
3849 IDirect3DDevice *device;
3850 HRESULT hr;
3851 HWND window;
3852 unsigned int i;
3853 DDSURFACEDESC ddsd;
3854 ULONG refcount;
3855 DWORD z_depth = 0;
3856 static const struct
3858 DWORD caps;
3859 const char *name;
3861 tests[] =
3864 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
3865 "videomemory offscreenplain"
3868 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3869 "systemmemory offscreenplain"
3872 DDSCAPS_PRIMARYSURFACE,
3873 "primary"
3876 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
3877 "videomemory texture"
3880 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
3881 "systemmemory texture"
3884 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
3885 "render target"
3888 DDSCAPS_ZBUFFER,
3889 "Z buffer"
3893 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3894 0, 0, 640, 480, 0, 0, 0, 0);
3895 ddraw = create_ddraw();
3896 ok(!!ddraw, "Failed to create a ddraw object.\n");
3897 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3899 skip("Failed to create a 3D device, skipping test.\n");
3900 IDirectDraw_Release(ddraw);
3901 DestroyWindow(window);
3902 return;
3904 z_depth = get_device_z_depth(device);
3905 ok(!!z_depth, "Failed to get device z depth.\n");
3906 IDirect3DDevice_Release(device);
3908 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
3910 memset(&ddsd, 0, sizeof(ddsd));
3911 ddsd.dwSize = sizeof(ddsd);
3912 ddsd.dwFlags = DDSD_CAPS;
3913 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
3915 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
3916 ddsd.dwWidth = 64;
3917 ddsd.dwHeight = 64;
3919 if (tests[i].caps & DDSCAPS_ZBUFFER)
3921 ddsd.dwFlags |= DDSD_ZBUFFERBITDEPTH;
3922 U2(ddsd).dwZBufferBitDepth = z_depth;
3924 ddsd.ddsCaps.dwCaps = tests[i].caps;
3926 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3927 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
3929 memset(&ddsd, 0, sizeof(ddsd));
3930 ddsd.dwSize = sizeof(ddsd);
3931 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
3932 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
3933 if (SUCCEEDED(hr))
3935 hr = IDirectDrawSurface_Unlock(surface, NULL);
3936 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
3939 IDirectDrawSurface_Release(surface);
3942 refcount = IDirectDraw_Release(ddraw);
3943 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
3944 DestroyWindow(window);
3947 static void test_surface_discard(void)
3949 IDirectDraw *ddraw;
3950 IDirect3DDevice *device;
3951 HRESULT hr;
3952 HWND window;
3953 DDSURFACEDESC ddsd;
3954 IDirectDrawSurface *surface, *target;
3955 void *addr;
3956 static const struct
3958 DWORD caps;
3959 BOOL discard;
3961 tests[] =
3963 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, TRUE},
3964 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, FALSE},
3965 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, TRUE},
3966 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, FALSE},
3968 unsigned int i;
3970 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3971 0, 0, 640, 480, 0, 0, 0, 0);
3972 ddraw = create_ddraw();
3973 ok(!!ddraw, "Failed to create a ddraw object.\n");
3974 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3976 skip("Failed to create a 3D device, skipping test.\n");
3977 IDirectDraw_Release(ddraw);
3978 DestroyWindow(window);
3979 return;
3982 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&target);
3983 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3985 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
3987 BOOL discarded;
3989 memset(&ddsd, 0, sizeof(ddsd));
3990 ddsd.dwSize = sizeof(ddsd);
3991 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3992 ddsd.ddsCaps.dwCaps = tests[i].caps;
3993 ddsd.dwWidth = 64;
3994 ddsd.dwHeight = 64;
3995 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3996 if (FAILED(hr))
3998 skip("Failed to create surface, skipping.\n");
3999 continue;
4002 memset(&ddsd, 0, sizeof(ddsd));
4003 ddsd.dwSize = sizeof(ddsd);
4004 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4005 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4006 addr = ddsd.lpSurface;
4007 hr = IDirectDrawSurface_Unlock(surface, NULL);
4008 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4010 memset(&ddsd, 0, sizeof(ddsd));
4011 ddsd.dwSize = sizeof(ddsd);
4012 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL);
4013 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4014 discarded = ddsd.lpSurface != addr;
4015 hr = IDirectDrawSurface_Unlock(surface, NULL);
4016 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4018 hr = IDirectDrawSurface_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
4019 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
4021 memset(&ddsd, 0, sizeof(ddsd));
4022 ddsd.dwSize = sizeof(ddsd);
4023 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL);
4024 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4025 discarded |= ddsd.lpSurface != addr;
4026 hr = IDirectDrawSurface_Unlock(surface, NULL);
4027 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4029 IDirectDrawSurface_Release(surface);
4031 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
4032 * AMD r500, evergreen). Windows XP, at least on AMD r200, never changes the pointer. */
4033 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
4036 IDirectDrawSurface_Release(target);
4037 IDirect3DDevice_Release(device);
4038 IDirectDraw_Release(ddraw);
4039 DestroyWindow(window);
4042 static void test_flip(void)
4044 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
4045 IDirectDrawSurface *primary, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
4046 DDSCAPS caps = {DDSCAPS_FLIP};
4047 DDSURFACEDESC surface_desc;
4048 BOOL sysmem_primary;
4049 IDirectDraw *ddraw;
4050 D3DCOLOR color;
4051 ULONG refcount;
4052 HWND window;
4053 DDBLTFX fx;
4054 HRESULT hr;
4056 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4057 0, 0, 640, 480, 0, 0, 0, 0);
4058 ddraw = create_ddraw();
4059 ok(!!ddraw, "Failed to create a ddraw object.\n");
4061 hr = set_display_mode(ddraw, 640, 480);
4062 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
4063 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4064 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4066 memset(&surface_desc, 0, sizeof(surface_desc));
4067 surface_desc.dwSize = sizeof(surface_desc);
4068 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4069 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4070 surface_desc.dwBackBufferCount = 3;
4071 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
4072 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4074 memset(&surface_desc, 0, sizeof(surface_desc));
4075 surface_desc.dwSize = sizeof(surface_desc);
4076 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &surface_desc);
4077 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4078 ok((surface_desc.ddsCaps.dwCaps & ~placement)
4079 == (DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX),
4080 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4081 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
4083 hr = IDirectDrawSurface_GetAttachedSurface(primary, &caps, &backbuffer1);
4084 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4085 memset(&surface_desc, 0, sizeof(surface_desc));
4086 surface_desc.dwSize = sizeof(surface_desc);
4087 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer1, &surface_desc);
4088 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4089 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
4090 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_BACKBUFFER),
4091 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4093 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
4094 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4095 memset(&surface_desc, 0, sizeof(surface_desc));
4096 surface_desc.dwSize = sizeof(surface_desc);
4097 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer2, &surface_desc);
4098 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4099 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
4100 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
4101 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4103 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
4104 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4105 memset(&surface_desc, 0, sizeof(surface_desc));
4106 surface_desc.dwSize = sizeof(surface_desc);
4107 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer3, &surface_desc);
4108 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4109 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
4110 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
4111 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4113 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer3, &caps, &surface);
4114 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4115 ok(surface == primary, "Got unexpected surface %p, expected %p.\n", surface, primary);
4116 IDirectDrawSurface_Release(surface);
4118 memset(&surface_desc, 0, sizeof(surface_desc));
4119 surface_desc.dwSize = sizeof(surface_desc);
4120 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4121 surface_desc.ddsCaps.dwCaps = 0;
4122 surface_desc.dwWidth = 640;
4123 surface_desc.dwHeight = 480;
4124 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4125 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4126 hr = IDirectDrawSurface_Flip(primary, surface, DDFLIP_WAIT);
4127 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4128 IDirectDrawSurface_Release(surface);
4130 hr = IDirectDrawSurface_Flip(primary, primary, DDFLIP_WAIT);
4131 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4132 hr = IDirectDrawSurface_Flip(backbuffer1, NULL, DDFLIP_WAIT);
4133 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4134 hr = IDirectDrawSurface_Flip(backbuffer2, NULL, DDFLIP_WAIT);
4135 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4136 hr = IDirectDrawSurface_Flip(backbuffer3, NULL, DDFLIP_WAIT);
4137 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4139 memset(&fx, 0, sizeof(fx));
4140 fx.dwSize = sizeof(fx);
4141 U5(fx).dwFillColor = 0xffff0000;
4142 hr = IDirectDrawSurface_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4143 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4144 U5(fx).dwFillColor = 0xff00ff00;
4145 hr = IDirectDrawSurface_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4146 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4147 U5(fx).dwFillColor = 0xff0000ff;
4148 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4149 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4151 hr = IDirectDrawSurface_Flip(primary, NULL, DDFLIP_WAIT);
4152 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4153 color = get_surface_color(backbuffer1, 320, 240);
4154 /* The testbot seems to just copy the contents of one surface to all the
4155 * others, instead of properly flipping. */
4156 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
4157 "Got unexpected color 0x%08x.\n", color);
4158 color = get_surface_color(backbuffer2, 320, 240);
4159 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
4160 U5(fx).dwFillColor = 0xffff0000;
4161 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4162 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4164 hr = IDirectDrawSurface_Flip(primary, NULL, DDFLIP_WAIT);
4165 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4166 color = get_surface_color(backbuffer1, 320, 240);
4167 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
4168 "Got unexpected color 0x%08x.\n", color);
4169 color = get_surface_color(backbuffer2, 320, 240);
4170 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
4171 U5(fx).dwFillColor = 0xff00ff00;
4172 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4173 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4175 hr = IDirectDrawSurface_Flip(primary, NULL, DDFLIP_WAIT);
4176 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4177 color = get_surface_color(backbuffer1, 320, 240);
4178 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
4179 "Got unexpected color 0x%08x.\n", color);
4180 color = get_surface_color(backbuffer2, 320, 240);
4181 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
4182 U5(fx).dwFillColor = 0xff0000ff;
4183 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4184 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4186 hr = IDirectDrawSurface_Flip(primary, backbuffer1, DDFLIP_WAIT);
4187 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4188 color = get_surface_color(backbuffer2, 320, 240);
4189 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
4190 "Got unexpected color 0x%08x.\n", color);
4191 color = get_surface_color(backbuffer3, 320, 240);
4192 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
4193 U5(fx).dwFillColor = 0xffff0000;
4194 hr = IDirectDrawSurface_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4195 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4197 hr = IDirectDrawSurface_Flip(primary, backbuffer2, DDFLIP_WAIT);
4198 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4199 color = get_surface_color(backbuffer1, 320, 240);
4200 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
4201 color = get_surface_color(backbuffer3, 320, 240);
4202 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
4203 "Got unexpected color 0x%08x.\n", color);
4204 U5(fx).dwFillColor = 0xff00ff00;
4205 hr = IDirectDrawSurface_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4206 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4208 hr = IDirectDrawSurface_Flip(primary, backbuffer3, DDFLIP_WAIT);
4209 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4210 color = get_surface_color(backbuffer1, 320, 240);
4211 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
4212 "Got unexpected color 0x%08x.\n", color);
4213 color = get_surface_color(backbuffer2, 320, 240);
4214 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
4216 IDirectDrawSurface_Release(backbuffer3);
4217 IDirectDrawSurface_Release(backbuffer2);
4218 IDirectDrawSurface_Release(backbuffer1);
4219 IDirectDrawSurface_Release(primary);
4220 refcount = IDirectDraw_Release(ddraw);
4221 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4222 DestroyWindow(window);
4225 static void test_sysmem_overlay(void)
4227 IDirectDraw *ddraw;
4228 HWND window;
4229 HRESULT hr;
4230 DDSURFACEDESC ddsd;
4231 IDirectDrawSurface *surface;
4232 ULONG ref;
4234 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4235 0, 0, 640, 480, 0, 0, 0, 0);
4236 ddraw = create_ddraw();
4237 ok(!!ddraw, "Failed to create a ddraw object.\n");
4239 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4240 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4242 memset(&ddsd, 0, sizeof(ddsd));
4243 ddsd.dwSize = sizeof(ddsd);
4244 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
4245 ddsd.dwWidth = 16;
4246 ddsd.dwHeight = 16;
4247 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
4248 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
4249 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
4250 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
4251 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
4252 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
4253 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
4254 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
4255 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
4257 ref = IDirectDraw_Release(ddraw);
4258 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
4259 DestroyWindow(window);
4262 static void test_primary_palette(void)
4264 DDSCAPS surface_caps = {DDSCAPS_FLIP};
4265 IDirectDrawSurface *primary, *backbuffer;
4266 PALETTEENTRY palette_entries[256];
4267 IDirectDrawPalette *palette, *tmp;
4268 DDSURFACEDESC surface_desc;
4269 IDirectDraw *ddraw;
4270 DWORD palette_caps;
4271 ULONG refcount;
4272 HWND window;
4273 HRESULT hr;
4275 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4276 0, 0, 640, 480, 0, 0, 0, 0);
4277 ddraw = create_ddraw();
4278 ok(!!ddraw, "Failed to create a ddraw object.\n");
4279 if (FAILED(IDirectDraw_SetDisplayMode(ddraw, 640, 480, 8)))
4281 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
4282 IDirectDraw_Release(ddraw);
4283 DestroyWindow(window);
4284 return;
4286 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4287 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4289 memset(&surface_desc, 0, sizeof(surface_desc));
4290 surface_desc.dwSize = sizeof(surface_desc);
4291 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4292 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4293 surface_desc.dwBackBufferCount = 1;
4294 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
4295 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4296 hr = IDirectDrawSurface_GetAttachedSurface(primary, &surface_caps, &backbuffer);
4297 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4299 memset(palette_entries, 0, sizeof(palette_entries));
4300 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
4301 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
4302 refcount = get_refcount((IUnknown *)palette);
4303 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4305 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
4306 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
4307 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
4309 hr = IDirectDrawSurface_SetPalette(primary, palette);
4310 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
4312 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
4313 * and is generally somewhat broken with respect to 8 bpp / palette
4314 * handling. */
4315 if (SUCCEEDED(IDirectDrawSurface_GetPalette(backbuffer, &tmp)))
4317 win_skip("Broken palette handling detected, skipping tests.\n");
4318 IDirectDrawPalette_Release(tmp);
4319 IDirectDrawPalette_Release(palette);
4320 /* The Windows 8 testbot keeps extra references to the primary and
4321 * backbuffer while in 8 bpp mode. */
4322 hr = IDirectDraw_RestoreDisplayMode(ddraw);
4323 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
4324 goto done;
4327 refcount = get_refcount((IUnknown *)palette);
4328 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4330 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
4331 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
4332 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
4333 "Got unexpected palette caps %#x.\n", palette_caps);
4335 hr = IDirectDrawSurface_SetPalette(primary, NULL);
4336 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
4337 refcount = get_refcount((IUnknown *)palette);
4338 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4340 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
4341 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
4342 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
4344 hr = IDirectDrawSurface_SetPalette(primary, palette);
4345 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
4346 refcount = get_refcount((IUnknown *)palette);
4347 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4349 hr = IDirectDrawSurface_GetPalette(primary, &tmp);
4350 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
4351 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
4352 IDirectDrawPalette_Release(tmp);
4353 hr = IDirectDrawSurface_GetPalette(backbuffer, &tmp);
4354 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
4356 refcount = IDirectDrawPalette_Release(palette);
4357 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4358 refcount = IDirectDrawPalette_Release(palette);
4359 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4361 /* Note that this only seems to work when the palette is attached to the
4362 * primary surface. When attached to a regular surface, attempting to get
4363 * the palette here will cause an access violation. */
4364 hr = IDirectDrawSurface_GetPalette(primary, &tmp);
4365 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
4367 done:
4368 refcount = IDirectDrawSurface_Release(backbuffer);
4369 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4370 refcount = IDirectDrawSurface_Release(primary);
4371 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4372 refcount = IDirectDraw_Release(ddraw);
4373 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4374 DestroyWindow(window);
4377 static HRESULT WINAPI surface_counter(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
4379 UINT *surface_count = context;
4381 ++(*surface_count);
4382 IDirectDrawSurface_Release(surface);
4384 return DDENUMRET_OK;
4387 static void test_surface_attachment(void)
4389 IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
4390 DDSCAPS caps = {DDSCAPS_TEXTURE};
4391 DDSURFACEDESC surface_desc;
4392 IDirectDraw *ddraw;
4393 UINT surface_count;
4394 ULONG refcount;
4395 HWND window;
4396 HRESULT hr;
4398 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4399 0, 0, 640, 480, 0, 0, 0, 0);
4400 ddraw = create_ddraw();
4401 ok(!!ddraw, "Failed to create a ddraw object.\n");
4402 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4403 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4405 memset(&surface_desc, 0, sizeof(surface_desc));
4406 surface_desc.dwSize = sizeof(surface_desc);
4407 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
4408 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
4409 U2(surface_desc).dwMipMapCount = 3;
4410 surface_desc.dwWidth = 128;
4411 surface_desc.dwHeight = 128;
4412 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
4413 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4415 hr = IDirectDrawSurface_GetAttachedSurface(surface1, &caps, &surface2);
4416 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
4417 hr = IDirectDrawSurface_GetAttachedSurface(surface2, &caps, &surface3);
4418 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
4419 hr = IDirectDrawSurface_GetAttachedSurface(surface3, &caps, &surface4);
4420 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
4422 surface_count = 0;
4423 IDirectDrawSurface_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
4424 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
4425 surface_count = 0;
4426 IDirectDrawSurface_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
4427 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
4428 surface_count = 0;
4429 IDirectDrawSurface_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
4430 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
4432 memset(&surface_desc, 0, sizeof(surface_desc));
4433 surface_desc.dwSize = sizeof(surface_desc);
4434 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4435 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
4436 surface_desc.dwWidth = 16;
4437 surface_desc.dwHeight = 16;
4438 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
4439 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4441 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
4442 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4443 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
4444 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4445 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface4);
4446 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4447 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface3);
4448 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4449 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface4);
4450 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4451 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface2);
4452 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4454 IDirectDrawSurface_Release(surface4);
4456 memset(&surface_desc, 0, sizeof(surface_desc));
4457 surface_desc.dwSize = sizeof(surface_desc);
4458 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4459 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
4460 surface_desc.dwWidth = 16;
4461 surface_desc.dwHeight = 16;
4462 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
4463 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4465 if (SUCCEEDED(hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4)))
4467 skip("Running on refrast, skipping some tests.\n");
4468 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface4);
4469 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4471 else
4473 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4474 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
4475 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4476 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface4);
4477 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4478 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface3);
4479 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4480 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface4);
4481 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4482 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface2);
4483 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4486 IDirectDrawSurface_Release(surface4);
4487 IDirectDrawSurface_Release(surface3);
4488 IDirectDrawSurface_Release(surface2);
4489 IDirectDrawSurface_Release(surface1);
4491 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4492 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4494 /* Try a single primary and two offscreen plain surfaces. */
4495 memset(&surface_desc, 0, sizeof(surface_desc));
4496 surface_desc.dwSize = sizeof(surface_desc);
4497 surface_desc.dwFlags = DDSD_CAPS;
4498 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
4499 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
4500 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4502 memset(&surface_desc, 0, sizeof(surface_desc));
4503 surface_desc.dwSize = sizeof(surface_desc);
4504 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4505 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4506 surface_desc.dwWidth = registry_mode.dmPelsWidth;
4507 surface_desc.dwHeight = registry_mode.dmPelsHeight;
4508 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
4509 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4511 memset(&surface_desc, 0, sizeof(surface_desc));
4512 surface_desc.dwSize = sizeof(surface_desc);
4513 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4514 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4515 surface_desc.dwWidth = registry_mode.dmPelsWidth;
4516 surface_desc.dwHeight = registry_mode.dmPelsHeight;
4517 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
4518 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4520 /* This one has a different size. */
4521 memset(&surface_desc, 0, sizeof(surface_desc));
4522 surface_desc.dwSize = sizeof(surface_desc);
4523 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4524 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4525 surface_desc.dwWidth = 128;
4526 surface_desc.dwHeight = 128;
4527 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
4528 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4530 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4531 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4532 /* Try the reverse without detaching first. */
4533 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
4534 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
4535 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
4536 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4538 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
4539 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4540 /* Try to detach reversed. */
4541 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
4542 ok(hr == DDERR_CANNOTDETACHSURFACE, "Got unexpected hr %#x.\n", hr);
4543 hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
4544 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4546 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3);
4547 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4548 hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
4549 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4551 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
4552 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4553 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
4554 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4556 IDirectDrawSurface_Release(surface4);
4557 IDirectDrawSurface_Release(surface3);
4558 IDirectDrawSurface_Release(surface2);
4559 IDirectDrawSurface_Release(surface1);
4561 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
4562 memset(&surface_desc, 0, sizeof(surface_desc));
4563 surface_desc.dwSize = sizeof(surface_desc);
4564 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
4565 surface_desc.dwWidth = 64;
4566 surface_desc.dwHeight = 64;
4567 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
4568 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
4569 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
4570 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 16;
4571 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xf800;
4572 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x07e0;
4573 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x001f;
4574 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
4575 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4576 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
4577 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4579 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4580 surface_desc.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
4581 U1(surface_desc.ddpfPixelFormat).dwZBufferBitDepth = 16;
4582 U3(surface_desc.ddpfPixelFormat).dwZBitMask = 0x0000ffff;
4583 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
4584 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4586 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4587 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4588 refcount = get_refcount((IUnknown *)surface2);
4589 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4590 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4591 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
4593 /* Attaching while already attached to other surface. */
4594 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface2);
4595 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4596 hr = IDirectDrawSurface_DeleteAttachedSurface(surface3, 0, surface2);
4597 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4598 IDirectDrawSurface_Release(surface3);
4600 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
4601 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4602 refcount = get_refcount((IUnknown *)surface2);
4603 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4605 /* Automatic detachment on release. */
4606 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4607 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4608 refcount = get_refcount((IUnknown *)surface2);
4609 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4610 refcount = IDirectDrawSurface_Release(surface1);
4611 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4612 refcount = IDirectDrawSurface_Release(surface2);
4613 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4614 refcount = IDirectDraw_Release(ddraw);
4615 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4616 DestroyWindow(window);
4619 static void test_pixel_format(void)
4621 HWND window, window2 = NULL;
4622 HDC hdc, hdc2 = NULL;
4623 HMODULE gl = NULL;
4624 int format, test_format;
4625 PIXELFORMATDESCRIPTOR pfd;
4626 IDirectDraw *ddraw = NULL;
4627 IDirectDrawClipper *clipper = NULL;
4628 DDSURFACEDESC ddsd;
4629 IDirectDrawSurface *primary = NULL;
4630 DDBLTFX fx;
4631 HRESULT hr;
4633 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4634 100, 100, 160, 160, NULL, NULL, NULL, NULL);
4635 if (!window)
4637 skip("Failed to create window\n");
4638 return;
4641 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4642 100, 100, 160, 160, NULL, NULL, NULL, NULL);
4644 hdc = GetDC(window);
4645 if (!hdc)
4647 skip("Failed to get DC\n");
4648 goto cleanup;
4651 if (window2)
4652 hdc2 = GetDC(window2);
4654 gl = LoadLibraryA("opengl32.dll");
4655 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
4657 format = GetPixelFormat(hdc);
4658 ok(format == 0, "new window has pixel format %d\n", format);
4660 ZeroMemory(&pfd, sizeof(pfd));
4661 pfd.nSize = sizeof(pfd);
4662 pfd.nVersion = 1;
4663 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
4664 pfd.iPixelType = PFD_TYPE_RGBA;
4665 pfd.iLayerType = PFD_MAIN_PLANE;
4666 format = ChoosePixelFormat(hdc, &pfd);
4667 if (format <= 0)
4669 skip("no pixel format available\n");
4670 goto cleanup;
4673 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
4675 skip("failed to set pixel format\n");
4676 goto cleanup;
4679 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
4681 skip("failed to set pixel format on second window\n");
4682 if (hdc2)
4684 ReleaseDC(window2, hdc2);
4685 hdc2 = NULL;
4689 ddraw = create_ddraw();
4690 ok(!!ddraw, "Failed to create a ddraw object.\n");
4692 test_format = GetPixelFormat(hdc);
4693 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4695 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4696 if (FAILED(hr))
4698 skip("Failed to set cooperative level, hr %#x.\n", hr);
4699 goto cleanup;
4702 test_format = GetPixelFormat(hdc);
4703 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4705 if (hdc2)
4707 hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
4708 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
4709 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
4710 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
4712 test_format = GetPixelFormat(hdc);
4713 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4715 test_format = GetPixelFormat(hdc2);
4716 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4719 memset(&ddsd, 0, sizeof(ddsd));
4720 ddsd.dwSize = sizeof(ddsd);
4721 ddsd.dwFlags = DDSD_CAPS;
4722 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
4724 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
4725 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
4727 test_format = GetPixelFormat(hdc);
4728 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4730 if (hdc2)
4732 test_format = GetPixelFormat(hdc2);
4733 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4736 if (clipper)
4738 hr = IDirectDrawSurface_SetClipper(primary, clipper);
4739 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
4741 test_format = GetPixelFormat(hdc);
4742 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4744 test_format = GetPixelFormat(hdc2);
4745 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4748 memset(&fx, 0, sizeof(fx));
4749 fx.dwSize = sizeof(fx);
4750 hr = IDirectDrawSurface_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4751 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
4753 test_format = GetPixelFormat(hdc);
4754 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4756 if (hdc2)
4758 test_format = GetPixelFormat(hdc2);
4759 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4762 cleanup:
4763 if (primary) IDirectDrawSurface_Release(primary);
4764 if (clipper) IDirectDrawClipper_Release(clipper);
4765 if (ddraw) IDirectDraw_Release(ddraw);
4766 if (gl) FreeLibrary(gl);
4767 if (hdc) ReleaseDC(window, hdc);
4768 if (hdc2) ReleaseDC(window2, hdc2);
4769 if (window) DestroyWindow(window);
4770 if (window2) DestroyWindow(window2);
4773 static void test_create_surface_pitch(void)
4775 IDirectDrawSurface *surface;
4776 DDSURFACEDESC surface_desc;
4777 IDirectDraw *ddraw;
4778 unsigned int i;
4779 ULONG refcount;
4780 HWND window;
4781 HRESULT hr;
4782 void *mem;
4784 static const struct
4786 DWORD placement;
4787 DWORD flags_in;
4788 DWORD pitch_in;
4789 HRESULT hr;
4790 DWORD flags_out;
4791 DWORD pitch_out32;
4792 DWORD pitch_out64;
4794 test_data[] =
4796 {DDSCAPS_VIDEOMEMORY, 0, 0, DD_OK,
4797 DDSD_PITCH, 0x100, 0x100},
4798 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x104, DD_OK,
4799 DDSD_PITCH, 0x100, 0x100},
4800 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
4801 DDSD_PITCH, 0x100, 0x100},
4802 {DDSCAPS_VIDEOMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
4803 0, 0, 0 },
4804 {DDSCAPS_SYSTEMMEMORY, 0, 0, DD_OK,
4805 DDSD_PITCH, 0x100, 0x0fc},
4806 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x104, DD_OK,
4807 DDSD_PITCH, 0x100, 0x0fc},
4808 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
4809 DDSD_PITCH, 0x100, 0x0fc},
4810 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
4811 DDSD_PITCH, 0x100, 0x0fc},
4812 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
4813 0, 0, 0 },
4814 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDPARAMS,
4815 0, 0, 0 },
4817 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
4819 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4820 0, 0, 640, 480, 0, 0, 0, 0);
4821 ddraw = create_ddraw();
4822 ok(!!ddraw, "Failed to create a ddraw object.\n");
4823 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4824 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4826 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
4828 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
4830 memset(&surface_desc, 0, sizeof(surface_desc));
4831 surface_desc.dwSize = sizeof(surface_desc);
4832 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
4833 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | test_data[i].placement;
4834 surface_desc.dwWidth = 63;
4835 surface_desc.dwHeight = 63;
4836 U1(surface_desc).lPitch = test_data[i].pitch_in;
4837 surface_desc.lpSurface = mem;
4838 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
4839 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
4840 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
4841 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
4842 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
4843 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
4844 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4845 ok(hr == test_data[i].hr || (test_data[i].placement == DDSCAPS_VIDEOMEMORY && hr == DDERR_NODIRECTDRAWHW),
4846 "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
4847 if (FAILED(hr))
4848 continue;
4850 memset(&surface_desc, 0, sizeof(surface_desc));
4851 surface_desc.dwSize = sizeof(surface_desc);
4852 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
4853 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
4854 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
4855 "Test %u: Got unexpected flags %#x, expected %#x.\n",
4856 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
4857 if (sizeof(void *) != sizeof(DWORD) && test_data[i].pitch_out32 != test_data[i].pitch_out64)
4858 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
4859 "Test %u: Got unexpected pitch %u, expected %u.\n",
4860 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
4861 else
4862 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
4863 "Test %u: Got unexpected pitch %u, expected %u.\n",
4864 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
4865 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
4867 IDirectDrawSurface_Release(surface);
4870 HeapFree(GetProcessHeap(), 0, mem);
4871 refcount = IDirectDraw_Release(ddraw);
4872 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4873 DestroyWindow(window);
4876 static void test_mipmap_lock(void)
4878 IDirectDrawSurface *surface, *surface2;
4879 DDSURFACEDESC surface_desc;
4880 IDirectDraw *ddraw;
4881 ULONG refcount;
4882 HWND window;
4883 HRESULT hr;
4884 DDSCAPS caps = {DDSCAPS_COMPLEX};
4885 DDCAPS hal_caps;
4887 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4888 0, 0, 640, 480, 0, 0, 0, 0);
4889 ddraw = create_ddraw();
4890 ok(!!ddraw, "Failed to create a ddraw object.\n");
4891 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4892 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4894 memset(&hal_caps, 0, sizeof(hal_caps));
4895 hal_caps.dwSize = sizeof(hal_caps);
4896 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
4897 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4898 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
4900 skip("Mipmapped textures not supported, skipping mipmap lock test.\n");
4901 IDirectDraw_Release(ddraw);
4902 DestroyWindow(window);
4903 return;
4906 memset(&surface_desc, 0, sizeof(surface_desc));
4907 surface_desc.dwSize = sizeof(surface_desc);
4908 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
4909 surface_desc.dwWidth = 4;
4910 surface_desc.dwHeight = 4;
4911 U2(surface_desc).dwMipMapCount = 2;
4912 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
4913 | DDSCAPS_SYSTEMMEMORY;
4914 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4915 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4916 hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &surface2);
4917 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4919 memset(&surface_desc, 0, sizeof(surface_desc));
4920 surface_desc.dwSize = sizeof(surface_desc);
4921 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, 0, NULL);
4922 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4923 memset(&surface_desc, 0, sizeof(surface_desc));
4924 surface_desc.dwSize = sizeof(surface_desc);
4925 hr = IDirectDrawSurface_Lock(surface2, NULL, &surface_desc, 0, NULL);
4926 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4927 IDirectDrawSurface_Unlock(surface2, NULL);
4928 IDirectDrawSurface_Unlock(surface, NULL);
4930 IDirectDrawSurface_Release(surface2);
4931 IDirectDrawSurface_Release(surface);
4932 refcount = IDirectDraw_Release(ddraw);
4933 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4934 DestroyWindow(window);
4937 static void test_palette_complex(void)
4939 IDirectDrawSurface *surface, *mipmap, *tmp;
4940 DDSURFACEDESC surface_desc;
4941 IDirectDraw *ddraw;
4942 IDirectDrawPalette *palette, *palette2, *palette_mipmap;
4943 ULONG refcount;
4944 HWND window;
4945 HRESULT hr;
4946 DDSCAPS caps = {DDSCAPS_COMPLEX};
4947 DDCAPS hal_caps;
4948 PALETTEENTRY palette_entries[256];
4949 unsigned int i;
4950 HDC dc;
4951 RGBQUAD rgbquad;
4952 UINT count;
4954 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4955 0, 0, 640, 480, 0, 0, 0, 0);
4956 ddraw = create_ddraw();
4957 ok(!!ddraw, "Failed to create a ddraw object.\n");
4958 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4959 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4961 memset(&hal_caps, 0, sizeof(hal_caps));
4962 hal_caps.dwSize = sizeof(hal_caps);
4963 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
4964 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4965 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
4967 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
4968 IDirectDraw_Release(ddraw);
4969 DestroyWindow(window);
4970 return;
4973 memset(&surface_desc, 0, sizeof(surface_desc));
4974 surface_desc.dwSize = sizeof(surface_desc);
4975 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
4976 surface_desc.dwWidth = 128;
4977 surface_desc.dwHeight = 128;
4978 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
4979 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
4980 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
4981 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
4982 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4983 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4985 memset(palette_entries, 0, sizeof(palette_entries));
4986 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
4987 palette_entries, &palette, NULL);
4988 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
4990 memset(palette_entries, 0, sizeof(palette_entries));
4991 palette_entries[1].peRed = 0xff;
4992 palette_entries[1].peGreen = 0x80;
4993 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
4994 palette_entries, &palette_mipmap, NULL);
4995 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
4997 palette2 = (void *)0xdeadbeef;
4998 hr = IDirectDrawSurface_GetPalette(surface, &palette2);
4999 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
5000 ok(!palette2, "Got unexpected palette %p.\n", palette2);
5001 hr = IDirectDrawSurface_SetPalette(surface, palette);
5002 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5003 hr = IDirectDrawSurface_GetPalette(surface, &palette2);
5004 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
5005 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
5006 IDirectDrawPalette_Release(palette2);
5008 mipmap = surface;
5009 IDirectDrawSurface_AddRef(mipmap);
5010 for (i = 0; i < 7; ++i)
5012 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
5013 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
5014 palette2 = (void *)0xdeadbeef;
5015 hr = IDirectDrawSurface_GetPalette(tmp, &palette2);
5016 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
5017 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
5019 hr = IDirectDrawSurface_SetPalette(tmp, palette_mipmap);
5020 ok(SUCCEEDED(hr), "Failed to set palette, i %u, hr %#x.\n", i, hr);
5022 hr = IDirectDrawSurface_GetPalette(tmp, &palette2);
5023 ok(SUCCEEDED(hr), "Failed to get palette, i %u, hr %#x.\n", i, hr);
5024 ok(palette_mipmap == palette2, "Got unexpected palette %p.\n", palette2);
5025 IDirectDrawPalette_Release(palette2);
5027 hr = IDirectDrawSurface_GetDC(tmp, &dc);
5028 ok(SUCCEEDED(hr), "Failed to get DC, i %u, hr %#x.\n", i, hr);
5029 count = GetDIBColorTable(dc, 1, 1, &rgbquad);
5030 ok(count == 1, "Expected count 1, got %u.\n", count);
5031 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x.\n", rgbquad.rgbRed);
5032 ok(rgbquad.rgbGreen == 0x80, "Expected rgbGreen = 0x80, got %#x.\n", rgbquad.rgbGreen);
5033 ok(rgbquad.rgbBlue == 0x0, "Expected rgbBlue = 0x0, got %#x.\n", rgbquad.rgbBlue);
5034 hr = IDirectDrawSurface_ReleaseDC(tmp, dc);
5035 ok(SUCCEEDED(hr), "Failed to release DC, i %u, hr %#x.\n", i, hr);
5037 IDirectDrawSurface_Release(mipmap);
5038 mipmap = tmp;
5041 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
5042 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
5043 IDirectDrawSurface_Release(mipmap);
5044 refcount = IDirectDrawSurface_Release(surface);
5045 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5046 refcount = IDirectDrawPalette_Release(palette_mipmap);
5047 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5048 refcount = IDirectDrawPalette_Release(palette);
5049 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5051 refcount = IDirectDraw_Release(ddraw);
5052 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5053 DestroyWindow(window);
5056 static void test_p8_rgb_blit(void)
5058 IDirectDrawSurface *src, *dst;
5059 DDSURFACEDESC surface_desc;
5060 IDirectDraw *ddraw;
5061 IDirectDrawPalette *palette;
5062 ULONG refcount;
5063 HWND window;
5064 HRESULT hr;
5065 PALETTEENTRY palette_entries[256];
5066 unsigned int x;
5067 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
5068 static const D3DCOLOR expected[] =
5070 0x00101010, 0x00010101, 0x00020202, 0x00030303,
5071 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
5073 D3DCOLOR color;
5075 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5076 0, 0, 640, 480, 0, 0, 0, 0);
5077 ddraw = create_ddraw();
5078 ok(!!ddraw, "Failed to create a ddraw object.\n");
5079 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5080 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5082 memset(palette_entries, 0, sizeof(palette_entries));
5083 palette_entries[1].peGreen = 0xff;
5084 palette_entries[2].peBlue = 0xff;
5085 palette_entries[3].peFlags = 0xff;
5086 palette_entries[4].peRed = 0xff;
5087 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5088 palette_entries, &palette, NULL);
5089 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5091 memset(&surface_desc, 0, sizeof(surface_desc));
5092 surface_desc.dwSize = sizeof(surface_desc);
5093 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5094 surface_desc.dwWidth = 8;
5095 surface_desc.dwHeight = 1;
5096 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5097 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5098 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
5099 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
5100 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src, NULL);
5101 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5103 memset(&surface_desc, 0, sizeof(surface_desc));
5104 surface_desc.dwSize = sizeof(surface_desc);
5105 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5106 surface_desc.dwWidth = 8;
5107 surface_desc.dwHeight = 1;
5108 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5109 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5110 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
5111 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
5112 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5113 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5114 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5115 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
5116 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst, NULL);
5117 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5119 memset(&surface_desc, 0, sizeof(surface_desc));
5120 surface_desc.dwSize = sizeof(surface_desc);
5121 hr = IDirectDrawSurface_Lock(src, NULL, &surface_desc, 0, NULL);
5122 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
5123 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
5124 hr = IDirectDrawSurface_Unlock(src, NULL);
5125 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
5127 hr = IDirectDrawSurface_SetPalette(src, palette);
5128 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5129 hr = IDirectDrawSurface_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
5130 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
5131 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
5132 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
5133 "Failed to blit, hr %#x.\n", hr);
5135 if (SUCCEEDED(hr))
5137 for (x = 0; x < sizeof(expected) / sizeof(*expected); x++)
5139 color = get_surface_color(dst, x, 0);
5140 todo_wine ok(compare_color(color, expected[x], 0),
5141 "Pixel %u: Got color %#x, expected %#x.\n",
5142 x, color, expected[x]);
5146 IDirectDrawSurface_Release(src);
5147 IDirectDrawSurface_Release(dst);
5148 IDirectDrawPalette_Release(palette);
5150 refcount = IDirectDraw_Release(ddraw);
5151 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5152 DestroyWindow(window);
5155 static void test_material(void)
5157 IDirect3DExecuteBuffer *execute_buffer;
5158 D3DMATERIALHANDLE mat_handle, tmp;
5159 D3DEXECUTEBUFFERDESC exec_desc;
5160 IDirect3DMaterial *material;
5161 IDirect3DViewport *viewport;
5162 IDirect3DDevice *device;
5163 IDirectDrawSurface *rt;
5164 IDirectDraw *ddraw;
5165 UINT inst_length;
5166 D3DCOLOR color;
5167 ULONG refcount;
5168 unsigned int i;
5169 HWND window;
5170 HRESULT hr;
5171 BOOL valid;
5172 void *ptr;
5174 static D3DVERTEX quad[] =
5176 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5177 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5178 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5179 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5181 static const struct
5183 BOOL material;
5184 D3DCOLOR expected_color;
5186 test_data[] =
5188 {TRUE, 0x0000ff00},
5189 {FALSE, 0x00ffffff},
5191 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
5193 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5194 0, 0, 640, 480, 0, 0, 0, 0);
5195 ddraw = create_ddraw();
5196 ok(!!ddraw, "Failed to create a ddraw object.\n");
5197 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
5199 skip("Failed to create a 3D device, skipping test.\n");
5200 DestroyWindow(window);
5201 return;
5204 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
5205 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
5207 material = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
5208 viewport = create_viewport(device, 0, 0, 640, 480);
5209 viewport_set_background(device, viewport, material);
5211 destroy_material(material);
5212 material = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
5213 hr = IDirect3DMaterial_GetHandle(material, device, &mat_handle);
5214 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
5216 memset(&exec_desc, 0, sizeof(exec_desc));
5217 exec_desc.dwSize = sizeof(exec_desc);
5218 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
5219 exec_desc.dwBufferSize = 1024;
5220 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
5222 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
5223 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
5225 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5227 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5228 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5230 memcpy(exec_desc.lpData, quad, sizeof(quad));
5231 ptr = ((BYTE *)exec_desc.lpData) + sizeof(quad);
5232 emit_set_ls(&ptr, D3DLIGHTSTATE_MATERIAL, test_data[i].material ? mat_handle : 0);
5233 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORMLIGHT, 0, 4);
5234 emit_tquad(&ptr, 0);
5235 emit_end(&ptr);
5236 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5237 inst_length -= sizeof(quad);
5239 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5240 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5242 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
5243 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5245 hr = IDirect3DDevice_BeginScene(device);
5246 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
5247 set_execute_data(execute_buffer, 4, sizeof(quad), inst_length);
5248 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5249 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5250 hr = IDirect3DDevice_EndScene(device);
5251 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
5252 color = get_surface_color(rt, 320, 240);
5253 if (test_data[i].material)
5254 todo_wine ok(compare_color(color, test_data[i].expected_color, 1)
5255 /* The Windows 8 testbot appears to return undefined results. */
5256 || broken(TRUE),
5257 "Got unexpected color 0x%08x, test %u.\n", color, i);
5258 else
5259 ok(compare_color(color, test_data[i].expected_color, 1),
5260 "Got unexpected color 0x%08x, test %u.\n", color, i);
5263 destroy_material(material);
5264 material = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
5265 hr = IDirect3DMaterial_GetHandle(material, device, &mat_handle);
5266 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
5268 hr = IDirect3DViewport_SetBackground(viewport, mat_handle);
5269 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
5270 hr = IDirect3DViewport_GetBackground(viewport, &tmp, &valid);
5271 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
5272 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
5273 ok(valid, "Got unexpected valid %#x.\n", valid);
5274 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5275 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5276 color = get_surface_color(rt, 320, 240);
5277 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5279 hr = IDirect3DViewport_SetBackground(viewport, 0);
5280 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
5281 hr = IDirect3DViewport_GetBackground(viewport, &tmp, &valid);
5282 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
5283 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
5284 ok(valid, "Got unexpected valid %#x.\n", valid);
5285 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5286 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5287 color = get_surface_color(rt, 320, 240);
5288 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5290 destroy_viewport(device, viewport);
5291 viewport = create_viewport(device, 0, 0, 640, 480);
5293 hr = IDirect3DViewport_GetBackground(viewport, &tmp, &valid);
5294 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
5295 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
5296 ok(!valid, "Got unexpected valid %#x.\n", valid);
5297 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5298 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5299 color = get_surface_color(rt, 320, 240);
5300 ok(compare_color(color, 0x00000000, 1), "Got unexpected color 0x%08x.\n", color);
5302 IDirect3DExecuteBuffer_Release(execute_buffer);
5303 destroy_viewport(device, viewport);
5304 destroy_material(material);
5305 IDirectDrawSurface_Release(rt);
5306 refcount = IDirect3DDevice_Release(device);
5307 ok(!refcount, "Device has %u references left.\n", refcount);
5308 refcount = IDirectDraw_Release(ddraw);
5309 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
5310 DestroyWindow(window);
5313 static void test_palette_gdi(void)
5315 IDirectDrawSurface *surface, *primary;
5316 DDSURFACEDESC surface_desc;
5317 IDirectDraw *ddraw;
5318 IDirectDrawPalette *palette, *palette2;
5319 ULONG refcount;
5320 HWND window;
5321 HRESULT hr;
5322 PALETTEENTRY palette_entries[256];
5323 UINT i;
5324 HDC dc;
5325 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
5326 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
5327 * not the point of this test. */
5328 static const RGBQUAD expected1[] =
5330 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
5331 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
5333 static const RGBQUAD expected2[] =
5335 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
5336 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
5338 static const RGBQUAD expected3[] =
5340 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
5341 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
5343 HPALETTE ddraw_palette_handle;
5344 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
5345 RGBQUAD rgbquad[255];
5346 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
5348 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5349 0, 0, 640, 480, 0, 0, 0, 0);
5350 ddraw = create_ddraw();
5351 ok(!!ddraw, "Failed to create a ddraw object.\n");
5352 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5353 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5355 memset(&surface_desc, 0, sizeof(surface_desc));
5356 surface_desc.dwSize = sizeof(surface_desc);
5357 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5358 surface_desc.dwWidth = 16;
5359 surface_desc.dwHeight = 16;
5360 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5361 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5362 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
5363 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
5364 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5365 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5367 /* Avoid colors from the Windows default palette. */
5368 memset(palette_entries, 0, sizeof(palette_entries));
5369 palette_entries[1].peRed = 0x01;
5370 palette_entries[2].peGreen = 0x02;
5371 palette_entries[3].peBlue = 0x03;
5372 palette_entries[4].peRed = 0x13;
5373 palette_entries[4].peGreen = 0x14;
5374 palette_entries[4].peBlue = 0x15;
5375 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5376 palette_entries, &palette, NULL);
5377 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5379 /* If there is no palette assigned and the display mode is not 8 bpp, some
5380 * drivers refuse to create a DC while others allow it. If a DC is created,
5381 * the DIB color table is uninitialized and contains random colors. No error
5382 * is generated when trying to read pixels and random garbage is returned.
5384 * The most likely explanation is that if the driver creates a DC, it (or
5385 * the higher-level runtime) uses GetSystemPaletteEntries to find the
5386 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
5387 * contains uninitialized garbage. See comments below for the P8 case. */
5389 hr = IDirectDrawSurface_SetPalette(surface, palette);
5390 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5391 hr = IDirectDrawSurface_GetDC(surface, &dc);
5392 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5393 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
5394 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
5395 "Got unexpected palette %p, expected %p.\n",
5396 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
5398 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5399 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5400 for (i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
5402 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
5403 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5404 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5405 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
5407 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5409 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5410 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5411 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5414 /* Update the palette while the DC is in use. This does not modify the DC. */
5415 palette_entries[4].peRed = 0x23;
5416 palette_entries[4].peGreen = 0x24;
5417 palette_entries[4].peBlue = 0x25;
5418 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
5419 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
5421 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
5422 ok(i == 1, "Expected count 1, got %u.\n", i);
5423 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
5424 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5425 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
5426 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
5428 /* Neither does re-setting the palette. */
5429 hr = IDirectDrawSurface_SetPalette(surface, NULL);
5430 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5431 hr = IDirectDrawSurface_SetPalette(surface, palette);
5432 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5434 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
5435 ok(i == 1, "Expected count 1, got %u.\n", i);
5436 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
5437 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5438 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
5439 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
5441 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
5442 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5444 /* Refresh the DC. This updates the palette. */
5445 hr = IDirectDrawSurface_GetDC(surface, &dc);
5446 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5447 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5448 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5449 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
5451 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
5452 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5453 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5454 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
5456 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5458 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5459 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5460 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5462 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
5463 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5465 refcount = IDirectDrawSurface_Release(surface);
5466 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5468 if (FAILED(IDirectDraw_SetDisplayMode(ddraw, 640, 480, 8)))
5470 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
5471 IDirectDrawPalette_Release(palette);
5472 IDirectDraw_Release(ddraw);
5473 DestroyWindow(window);
5474 return;
5476 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
5477 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
5478 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5480 memset(&surface_desc, 0, sizeof(surface_desc));
5481 surface_desc.dwSize = sizeof(surface_desc);
5482 surface_desc.dwFlags = DDSD_CAPS;
5483 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
5484 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
5485 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5487 hr = IDirectDrawSurface_SetPalette(primary, palette);
5488 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5489 hr = IDirectDrawSurface_GetDC(primary, &dc);
5490 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5491 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
5492 /* Windows 2000 on the testbot assigns a different palette to the primary. Refrast? */
5493 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE) || broken(TRUE),
5494 "Got unexpected palette %p, expected %p.\n",
5495 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
5496 SelectPalette(dc, ddraw_palette_handle, FALSE);
5498 /* The primary uses the system palette. In exclusive mode, the system palette matches
5499 * the ddraw palette attached to the primary, so the result is what you would expect
5500 * from a regular surface. Tests for the interaction between the ddraw palette and
5501 * the system palette are not included pending an application that depends on this.
5502 * The relation between those causes problems on Windows Vista and newer for games
5503 * like Age of Empires or StarCraft. Don't emulate it without a real need. */
5504 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5505 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5506 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
5508 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
5509 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5510 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5511 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
5513 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5515 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5516 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5517 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5519 hr = IDirectDrawSurface_ReleaseDC(primary, dc);
5520 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5522 memset(&surface_desc, 0, sizeof(surface_desc));
5523 surface_desc.dwSize = sizeof(surface_desc);
5524 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5525 surface_desc.dwWidth = 16;
5526 surface_desc.dwHeight = 16;
5527 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5528 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5529 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5531 /* Here the offscreen surface appears to use the primary's palette,
5532 * but in all likelihood it is actually the system palette. */
5533 hr = IDirectDrawSurface_GetDC(surface, &dc);
5534 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5535 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5536 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5537 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
5539 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
5540 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5541 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5542 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
5544 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5546 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5547 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5548 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5550 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
5551 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5553 /* On real hardware a change to the primary surface's palette applies immediately,
5554 * even on device contexts from offscreen surfaces that do not have their own
5555 * palette. On the testbot VMs this is not the case. Don't test this until we
5556 * know of an application that depends on this. */
5558 memset(palette_entries, 0, sizeof(palette_entries));
5559 palette_entries[1].peBlue = 0x40;
5560 palette_entries[2].peRed = 0x40;
5561 palette_entries[3].peGreen = 0x40;
5562 palette_entries[4].peRed = 0x12;
5563 palette_entries[4].peGreen = 0x34;
5564 palette_entries[4].peBlue = 0x56;
5565 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5566 palette_entries, &palette2, NULL);
5567 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5568 hr = IDirectDrawSurface_SetPalette(surface, palette2);
5569 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5571 /* A palette assigned to the offscreen surface overrides the primary / system
5572 * palette. */
5573 hr = IDirectDrawSurface_GetDC(surface, &dc);
5574 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5575 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5576 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5577 for (i = 0; i < sizeof(expected3) / sizeof(*expected3); i++)
5579 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
5580 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5581 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5582 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
5584 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5586 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5587 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5588 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5590 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
5591 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5593 refcount = IDirectDrawSurface_Release(surface);
5594 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5596 /* The Windows 8 testbot keeps extra references to the primary and
5597 * backbuffer while in 8 bpp mode. */
5598 hr = IDirectDraw_RestoreDisplayMode(ddraw);
5599 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
5601 refcount = IDirectDrawSurface_Release(primary);
5602 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5603 refcount = IDirectDrawPalette_Release(palette2);
5604 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5605 refcount = IDirectDrawPalette_Release(palette);
5606 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5607 refcount = IDirectDraw_Release(ddraw);
5608 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5609 DestroyWindow(window);
5612 static void test_palette_alpha(void)
5614 IDirectDrawSurface *surface;
5615 DDSURFACEDESC surface_desc;
5616 IDirectDraw *ddraw;
5617 IDirectDrawPalette *palette;
5618 ULONG refcount;
5619 HWND window;
5620 HRESULT hr;
5621 PALETTEENTRY palette_entries[256];
5622 unsigned int i;
5623 static const struct
5625 DWORD caps, flags;
5626 BOOL attach_allowed;
5627 const char *name;
5629 test_data[] =
5631 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
5632 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
5633 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
5636 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5637 0, 0, 640, 480, 0, 0, 0, 0);
5638 ddraw = create_ddraw();
5639 ok(!!ddraw, "Failed to create a ddraw object.\n");
5640 if (FAILED(IDirectDraw_SetDisplayMode(ddraw, 640, 480, 8)))
5642 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
5643 IDirectDraw_Release(ddraw);
5644 DestroyWindow(window);
5645 return;
5647 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5648 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5650 memset(palette_entries, 0, sizeof(palette_entries));
5651 palette_entries[1].peFlags = 0x42;
5652 palette_entries[2].peFlags = 0xff;
5653 palette_entries[3].peFlags = 0x80;
5654 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
5655 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5657 memset(palette_entries, 0x66, sizeof(palette_entries));
5658 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
5659 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
5660 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
5661 palette_entries[0].peFlags);
5662 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
5663 palette_entries[1].peFlags);
5664 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
5665 palette_entries[2].peFlags);
5666 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
5667 palette_entries[3].peFlags);
5669 IDirectDrawPalette_Release(palette);
5671 memset(palette_entries, 0, sizeof(palette_entries));
5672 palette_entries[1].peFlags = 0x42;
5673 palette_entries[1].peRed = 0xff;
5674 palette_entries[2].peFlags = 0xff;
5675 palette_entries[3].peFlags = 0x80;
5676 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
5677 palette_entries, &palette, NULL);
5678 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5680 memset(palette_entries, 0x66, sizeof(palette_entries));
5681 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
5682 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
5683 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
5684 palette_entries[0].peFlags);
5685 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
5686 palette_entries[1].peFlags);
5687 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
5688 palette_entries[2].peFlags);
5689 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
5690 palette_entries[3].peFlags);
5692 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
5694 memset(&surface_desc, 0, sizeof(surface_desc));
5695 surface_desc.dwSize = sizeof(surface_desc);
5696 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
5697 surface_desc.dwWidth = 128;
5698 surface_desc.dwHeight = 128;
5699 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
5700 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5701 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
5703 hr = IDirectDrawSurface_SetPalette(surface, palette);
5704 if (test_data[i].attach_allowed)
5705 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
5706 else
5707 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
5709 if (SUCCEEDED(hr))
5711 HDC dc;
5712 RGBQUAD rgbquad;
5713 UINT retval;
5715 hr = IDirectDrawSurface_GetDC(surface, &dc);
5716 ok(SUCCEEDED(hr) || broken(hr == DDERR_CANTCREATEDC) /* Win2k testbot */,
5717 "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
5718 if (SUCCEEDED(hr))
5720 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
5721 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
5722 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
5723 rgbquad.rgbRed, test_data[i].name);
5724 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
5725 rgbquad.rgbGreen, test_data[i].name);
5726 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
5727 rgbquad.rgbBlue, test_data[i].name);
5728 todo_wine ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
5729 rgbquad.rgbReserved, test_data[i].name);
5730 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
5731 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5734 IDirectDrawSurface_Release(surface);
5737 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
5738 memset(&surface_desc, 0, sizeof(surface_desc));
5739 surface_desc.dwSize = sizeof(surface_desc);
5740 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5741 surface_desc.dwWidth = 128;
5742 surface_desc.dwHeight = 128;
5743 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5744 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5745 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
5746 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
5747 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5748 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5749 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5750 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5751 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5752 hr = IDirectDrawSurface_SetPalette(surface, palette);
5753 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
5754 IDirectDrawSurface_Release(surface);
5756 /* The Windows 8 testbot keeps extra references to the primary
5757 * while in 8 bpp mode. */
5758 hr = IDirectDraw_RestoreDisplayMode(ddraw);
5759 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
5761 refcount = IDirectDrawPalette_Release(palette);
5762 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5763 refcount = IDirectDraw_Release(ddraw);
5764 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5765 DestroyWindow(window);
5768 static void test_lost_device(void)
5770 IDirectDrawSurface *surface;
5771 DDSURFACEDESC surface_desc;
5772 IDirectDraw *ddraw;
5773 ULONG refcount;
5774 HWND window;
5775 HRESULT hr;
5776 BOOL ret;
5778 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5779 0, 0, 640, 480, 0, 0, 0, 0);
5780 ddraw = create_ddraw();
5781 ok(!!ddraw, "Failed to create a ddraw object.\n");
5782 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5783 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5785 memset(&surface_desc, 0, sizeof(surface_desc));
5786 surface_desc.dwSize = sizeof(surface_desc);
5787 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
5788 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
5789 surface_desc.dwBackBufferCount = 1;
5790 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5791 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5793 hr = IDirectDrawSurface_IsLost(surface);
5794 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5795 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
5796 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5798 ret = SetForegroundWindow(GetDesktopWindow());
5799 ok(ret, "Failed to set foreground window.\n");
5800 hr = IDirectDrawSurface_IsLost(surface);
5801 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5802 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
5803 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5805 ret = SetForegroundWindow(window);
5806 ok(ret, "Failed to set foreground window.\n");
5807 hr = IDirectDrawSurface_IsLost(surface);
5808 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5809 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
5810 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5812 hr = restore_surfaces(ddraw);
5813 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5814 hr = IDirectDrawSurface_IsLost(surface);
5815 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5816 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
5817 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5819 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5820 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5821 hr = IDirectDrawSurface_IsLost(surface);
5822 todo_wine ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5823 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
5824 todo_wine ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
5826 /* Trying to restore the primary will crash, probably because flippable
5827 * surfaces can't exist in DDSCL_NORMAL. */
5828 IDirectDrawSurface_Release(surface);
5829 memset(&surface_desc, 0, sizeof(surface_desc));
5830 surface_desc.dwSize = sizeof(surface_desc);
5831 surface_desc.dwFlags = DDSD_CAPS;
5832 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
5833 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5834 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5836 hr = IDirectDrawSurface_IsLost(surface);
5837 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5839 ret = SetForegroundWindow(GetDesktopWindow());
5840 ok(ret, "Failed to set foreground window.\n");
5841 hr = IDirectDrawSurface_IsLost(surface);
5842 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5844 ret = SetForegroundWindow(window);
5845 ok(ret, "Failed to set foreground window.\n");
5846 hr = IDirectDrawSurface_IsLost(surface);
5847 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5849 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5850 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5851 hr = IDirectDrawSurface_IsLost(surface);
5852 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
5854 hr = restore_surfaces(ddraw);
5855 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5856 hr = IDirectDrawSurface_IsLost(surface);
5857 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
5859 IDirectDrawSurface_Release(surface);
5860 refcount = IDirectDraw_Release(ddraw);
5861 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5862 DestroyWindow(window);
5865 static void test_surface_desc_lock(void)
5867 IDirectDrawSurface *surface;
5868 DDSURFACEDESC surface_desc;
5869 IDirectDraw *ddraw;
5870 ULONG refcount;
5871 HWND window;
5872 HRESULT hr;
5874 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5875 0, 0, 640, 480, 0, 0, 0, 0);
5876 ddraw = create_ddraw();
5877 ok(!!ddraw, "Failed to create a ddraw object.\n");
5878 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5879 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5881 memset(&surface_desc, 0, sizeof(surface_desc));
5882 surface_desc.dwSize = sizeof(surface_desc);
5883 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5884 surface_desc.dwWidth = 16;
5885 surface_desc.dwHeight = 16;
5886 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5887 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5888 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5890 memset(&surface_desc, 0xaa, sizeof(surface_desc));
5891 surface_desc.dwSize = sizeof(surface_desc);
5892 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
5893 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5894 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
5896 memset(&surface_desc, 0xaa, sizeof(surface_desc));
5897 surface_desc.dwSize = sizeof(surface_desc);
5898 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, 0, NULL);
5899 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5900 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
5901 memset(&surface_desc, 0xaa, sizeof(surface_desc));
5902 surface_desc.dwSize = sizeof(surface_desc);
5903 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
5904 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5905 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
5906 hr = IDirectDrawSurface_Unlock(surface, NULL);
5907 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5909 memset(&surface_desc, 0xaa, sizeof(surface_desc));
5910 surface_desc.dwSize = sizeof(surface_desc);
5911 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
5912 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5913 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
5915 IDirectDrawSurface_Release(surface);
5916 refcount = IDirectDraw_Release(ddraw);
5917 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5918 DestroyWindow(window);
5921 static void test_texturemapblend(void)
5923 HRESULT hr;
5924 DDSURFACEDESC ddsd;
5925 D3DEXECUTEBUFFERDESC exec_desc;
5926 DDBLTFX fx;
5927 static RECT rect = {0, 0, 64, 128};
5928 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
5929 DDCOLORKEY ckey;
5930 IDirectDrawSurface *surface, *rt;
5931 IDirect3DTexture *texture;
5932 D3DTEXTUREHANDLE texture_handle;
5933 HWND window;
5934 IDirectDraw *ddraw;
5935 IDirect3DDevice *device;
5936 IDirect3DMaterial *material;
5937 IDirect3DViewport *viewport;
5938 IDirect3DExecuteBuffer *execute_buffer;
5939 UINT inst_length;
5940 void *ptr;
5941 ULONG ref;
5942 D3DCOLOR color;
5944 static const D3DTLVERTEX test1_quads[] =
5946 {{0.0f}, {0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {0.0f}, {0.0f}},
5947 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {0.0f}, {1.0f}},
5948 {{640.0f}, {0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {1.0f}, {0.0f}},
5949 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {1.0f}, {1.0f}},
5950 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {0.0f}, {0.0f}},
5951 {{0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {0.0f}, {1.0f}},
5952 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {1.0f}, {0.0f}},
5953 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {1.0f}, {1.0f}},
5955 test2_quads[] =
5957 {{0.0f}, {0.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {0.0f}, {0.0f}},
5958 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {0.0f}, {1.0f}},
5959 {{640.0f}, {0.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {1.0f}, {0.0f}},
5960 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {1.0f}, {1.0f}},
5961 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {0.0f}, {0.0f}},
5962 {{0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {0.0f}, {1.0f}},
5963 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {1.0f}, {0.0f}},
5964 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {1.0f}, {1.0f}},
5967 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5968 0, 0, 640, 480, 0, 0, 0, 0);
5969 ddraw = create_ddraw();
5970 ok(!!ddraw, "Failed to create a ddraw object.\n");
5971 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
5973 skip("Failed to create a 3D device, skipping test.\n");
5974 DestroyWindow(window);
5975 IDirectDraw_Release(ddraw);
5976 return;
5979 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
5980 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
5982 material = create_diffuse_material(device, 0.0f, 0.0f, 0.0f, 1.0f);
5983 viewport = create_viewport(device, 0, 0, 640, 480);
5984 viewport_set_background(device, viewport, material);
5986 memset(&exec_desc, 0, sizeof(exec_desc));
5987 exec_desc.dwSize = sizeof(exec_desc);
5988 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
5989 exec_desc.dwBufferSize = 1024;
5990 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
5991 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
5992 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
5994 /* Test alpha with DDPF_ALPHAPIXELS texture - should be taken from texture alpha channel.
5996 * The vertex alpha is completely ignored in this case, so case 1 and 2 combined are not
5997 * a D3DTOP_MODULATE with texture alpha = 0xff in case 2 (no alpha in texture). */
5998 memset(&ddsd, 0, sizeof(ddsd));
5999 ddsd.dwSize = sizeof(ddsd);
6000 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6001 ddsd.dwHeight = 128;
6002 ddsd.dwWidth = 128;
6003 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6004 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6005 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
6006 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
6007 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6008 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6009 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6010 U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
6011 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6012 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6014 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6015 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6016 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6017 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6019 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6020 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6022 memset(&fx, 0, sizeof(fx));
6023 fx.dwSize = sizeof(fx);
6024 U5(fx).dwFillColor = 0xff0000ff;
6025 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6026 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6027 U5(fx).dwFillColor = 0x800000ff;
6028 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6029 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6031 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6032 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6034 memcpy(exec_desc.lpData, test1_quads, sizeof(test1_quads));
6036 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test1_quads);
6037 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6038 emit_set_rs(&ptr, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
6039 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
6040 emit_set_rs(&ptr, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
6041 emit_set_rs(&ptr, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
6042 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
6043 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATE);
6044 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6046 emit_tquad(&ptr, 0);
6047 emit_tquad(&ptr, 4);
6048 emit_end(&ptr);
6050 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6051 inst_length -= sizeof(test1_quads);
6052 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6053 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6054 set_execute_data(execute_buffer, 8, sizeof(test1_quads), inst_length);
6056 hr = IDirect3DDevice_BeginScene(device);
6057 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6058 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6059 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6060 hr = IDirect3DDevice_EndScene(device);
6061 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6063 color = get_surface_color(rt, 5, 5);
6064 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6065 color = get_surface_color(rt, 400, 5);
6066 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6067 color = get_surface_color(rt, 5, 245);
6068 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6069 color = get_surface_color(rt, 400, 245);
6070 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6072 IDirect3DTexture_Release(texture);
6073 ref = IDirectDrawSurface_Release(surface);
6074 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6076 /* Test alpha with texture that has no alpha channel - alpha should be taken from diffuse vertex color. */
6077 memset(&ddsd, 0, sizeof(ddsd));
6078 ddsd.dwSize = sizeof(ddsd);
6079 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6080 ddsd.dwHeight = 128;
6081 ddsd.dwWidth = 128;
6082 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6083 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6084 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
6085 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
6086 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6087 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6088 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6090 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6091 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6093 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6094 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6095 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6096 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6098 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6099 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6101 U5(fx).dwFillColor = 0xff0000ff;
6102 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6103 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6104 U5(fx).dwFillColor = 0x800000ff;
6105 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6106 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6108 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6109 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6111 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test1_quads);
6112 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6113 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6115 emit_tquad(&ptr, 0);
6116 emit_tquad(&ptr, 4);
6117 emit_end(&ptr);
6119 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6120 inst_length -= sizeof(test1_quads);
6121 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6122 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6123 set_execute_data(execute_buffer, 8, sizeof(test1_quads), inst_length);
6125 hr = IDirect3DDevice_BeginScene(device);
6126 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6127 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6128 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6129 hr = IDirect3DDevice_EndScene(device);
6130 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6132 color = get_surface_color(rt, 5, 5);
6133 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6134 color = get_surface_color(rt, 400, 5);
6135 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6136 color = get_surface_color(rt, 5, 245);
6137 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6138 color = get_surface_color(rt, 400, 245);
6139 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6141 IDirect3DTexture_Release(texture);
6142 ref = IDirectDrawSurface_Release(surface);
6143 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6145 /* Test RGB - should multiply color components from diffuse vertex color and texture. */
6146 memset(&ddsd, 0, sizeof(ddsd));
6147 ddsd.dwSize = sizeof(ddsd);
6148 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6149 ddsd.dwHeight = 128;
6150 ddsd.dwWidth = 128;
6151 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6152 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6153 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
6154 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
6155 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6156 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6157 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6158 U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
6159 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6160 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6162 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6163 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6164 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6165 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6167 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6168 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6170 U5(fx).dwFillColor = 0x00ffffff;
6171 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6172 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6173 U5(fx).dwFillColor = 0x00ffff80;
6174 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6175 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6177 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6178 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6180 memcpy(exec_desc.lpData, test2_quads, sizeof(test2_quads));
6182 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test2_quads);
6183 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6184 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
6185 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6187 emit_tquad(&ptr, 0);
6188 emit_tquad(&ptr, 4);
6189 emit_end(&ptr);
6191 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6192 inst_length -= sizeof(test2_quads);
6193 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6194 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6195 set_execute_data(execute_buffer, 8, sizeof(test2_quads), inst_length);
6197 hr = IDirect3DDevice_BeginScene(device);
6198 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6199 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6200 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6201 hr = IDirect3DDevice_EndScene(device);
6202 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6204 /* WARP (Win8 testbot) emulates color keying with the alpha channel like Wine does,
6205 * but even applies it when there's no color key assigned. The surface alpha is zero
6206 * here, so nothing gets drawn.
6208 * The ddraw2 version of this test draws these quads with color keying off due to
6209 * different defaults in ddraw1 and ddraw2. */
6210 color = get_surface_color(rt, 5, 5);
6211 ok(compare_color(color, 0x00ff0040, 2) || broken(compare_color(color, 0x00000000, 1)),
6212 "Got unexpected color 0x%08x.\n", color);
6213 color = get_surface_color(rt, 400, 5);
6214 ok(compare_color(color, 0x00ff0080, 2) || broken(compare_color(color, 0x00000000, 1)),
6215 "Got unexpected color 0x%08x.\n", color);
6216 color = get_surface_color(rt, 5, 245);
6217 ok(compare_color(color, 0x00800080, 2) || broken(compare_color(color, 0x00000000, 1)),
6218 "Got unexpected color 0x%08x.\n", color);
6219 color = get_surface_color(rt, 400, 245);
6220 ok(compare_color(color, 0x008000ff, 2) || broken(compare_color(color, 0x00000000, 1)),
6221 "Got unexpected color 0x%08x.\n", color);
6223 IDirect3DTexture_Release(texture);
6224 ref = IDirectDrawSurface_Release(surface);
6225 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6227 /* Test alpha again, now with color keyed texture (colorkey emulation in wine can interfere). */
6228 memset(&ddsd, 0, sizeof(ddsd));
6229 ddsd.dwSize = sizeof(ddsd);
6230 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6231 ddsd.dwHeight = 128;
6232 ddsd.dwWidth = 128;
6233 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6234 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6235 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
6236 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 16;
6237 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xf800;
6238 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x07e0;
6239 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x001f;
6241 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6242 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6244 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6245 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6246 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6247 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6249 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6250 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6252 U5(fx).dwFillColor = 0xf800;
6253 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6254 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6255 U5(fx).dwFillColor = 0x001f;
6256 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6257 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6259 ckey.dwColorSpaceLowValue = 0x001f;
6260 ckey.dwColorSpaceHighValue = 0x001f;
6261 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
6262 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
6264 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6265 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6267 memcpy(exec_desc.lpData, test1_quads, sizeof(test1_quads));
6269 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test1_quads);
6270 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6271 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
6272 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6273 /* This is supposed to be on by default on version 1 devices,
6274 * but for some reason it randomly defaults to FALSE on the W8
6275 * testbot. This is either the fault of Windows 8 or the WARP
6276 * driver. */
6277 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
6279 emit_tquad(&ptr, 0);
6280 emit_tquad(&ptr, 4);
6281 emit_end(&ptr);
6283 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6284 inst_length -= sizeof(test1_quads);
6285 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6286 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6287 set_execute_data(execute_buffer, 8, sizeof(test1_quads), inst_length);
6289 hr = IDirect3DDevice_BeginScene(device);
6290 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6291 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6292 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6293 hr = IDirect3DDevice_EndScene(device);
6294 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6296 color = get_surface_color(rt, 5, 5);
6297 ok(compare_color(color, 0x00000000, 2), "Got unexpected color 0x%08x.\n", color);
6298 color = get_surface_color(rt, 400, 5);
6299 ok(compare_color(color, 0x00ff0000, 2), "Got unexpected color 0x%08x.\n", color);
6300 color = get_surface_color(rt, 5, 245);
6301 ok(compare_color(color, 0x00000000, 2), "Got unexpected color 0x%08x.\n", color);
6302 color = get_surface_color(rt, 400, 245);
6303 ok(compare_color(color, 0x00800000, 2), "Got unexpected color 0x%08x.\n", color);
6305 IDirect3DTexture_Release(texture);
6306 ref = IDirectDrawSurface_Release(surface);
6307 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6309 ref = IDirect3DExecuteBuffer_Release(execute_buffer);
6310 ok(ref == 0, "Execute buffer not properly released, refcount %u.\n", ref);
6311 destroy_viewport(device, viewport);
6312 ref = IDirect3DMaterial_Release(material);
6313 ok(ref == 0, "Material not properly released, refcount %u.\n", ref);
6314 IDirectDrawSurface_Release(rt);
6315 IDirect3DDevice_Release(device);
6316 ref = IDirectDraw_Release(ddraw);
6317 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6318 DestroyWindow(window);
6321 static void test_viewport_clear_rect(void)
6323 HRESULT hr;
6324 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
6325 static D3DRECT clear_rect2 = {{90}, {90}, {110}, {110}};
6326 IDirectDrawSurface *rt;
6327 HWND window;
6328 IDirectDraw *ddraw;
6329 IDirect3DDevice *device;
6330 IDirect3DMaterial *red, *green;
6331 IDirect3DViewport *viewport, *viewport2;
6332 ULONG ref;
6333 D3DCOLOR color;
6335 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6336 0, 0, 640, 480, 0, 0, 0, 0);
6337 ddraw = create_ddraw();
6338 ok(!!ddraw, "Failed to create a ddraw object.\n");
6339 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
6341 skip("Failed to create a 3D device, skipping test.\n");
6342 DestroyWindow(window);
6343 IDirectDraw_Release(ddraw);
6344 return;
6347 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
6348 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6350 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
6351 viewport = create_viewport(device, 0, 0, 640, 480);
6352 viewport_set_background(device, viewport, red);
6353 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6354 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6356 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
6357 viewport2 = create_viewport(device, 100, 100, 20, 20);
6358 viewport_set_background(device, viewport2, green);
6359 hr = IDirect3DViewport_Clear(viewport2, 1, &clear_rect2, D3DCLEAR_TARGET);
6360 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6362 color = get_surface_color(rt, 85, 85); /* Outside both. */
6363 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6364 color = get_surface_color(rt, 95, 95); /* Outside vp, inside rect. */
6365 /* AMD GPUs ignore the viewport dimensions and only care about the rectangle. */
6366 ok(compare_color(color, 0x00ff0000, 1) || broken(compare_color(color, 0x0000ff00, 1)),
6367 "Got unexpected color 0x%08x.\n", color);
6368 color = get_surface_color(rt, 105, 105); /* Inside both. */
6369 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
6370 color = get_surface_color(rt, 115, 115); /* Inside vp, outside rect. */
6371 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6372 color = get_surface_color(rt, 125, 125); /* Outside both. */
6373 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6375 destroy_viewport(device, viewport2);
6376 destroy_material(green);
6377 destroy_viewport(device, viewport);
6378 destroy_material(red);
6379 IDirectDrawSurface_Release(rt);
6380 IDirect3DDevice_Release(device);
6381 ref = IDirectDraw_Release(ddraw);
6382 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6383 DestroyWindow(window);
6386 static void test_color_fill(void)
6388 HRESULT hr;
6389 IDirect3DDevice *device;
6390 IDirectDraw *ddraw;
6391 IDirectDrawSurface *surface, *surface2;
6392 DDSURFACEDESC surface_desc;
6393 ULONG refcount;
6394 HWND window;
6395 unsigned int i;
6396 DDBLTFX fx;
6397 RECT rect = {5, 5, 7, 7};
6398 DWORD *color;
6399 DWORD num_fourcc_codes, *fourcc_codes;
6400 DDCAPS hal_caps;
6401 BOOL support_uyvy = FALSE, support_yuy2 = FALSE;
6402 static const struct
6404 DWORD caps;
6405 HRESULT colorfill_hr, depthfill_hr;
6406 BOOL rop_success;
6407 const char *name;
6408 DWORD result;
6409 BOOL check_result;
6410 DDPIXELFORMAT format;
6412 tests[] =
6415 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
6416 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain RGB", 0xdeadbeef, TRUE,
6418 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
6419 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
6423 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
6424 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain RGB", 0xdeadbeef, TRUE,
6426 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
6427 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
6431 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
6432 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem texture RGB", 0xdeadbeef, TRUE,
6434 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
6435 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
6439 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
6440 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem texture RGB", 0xdeadbeef, TRUE,
6442 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
6443 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
6447 DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY,
6448 DDERR_INVALIDPARAMS, DD_OK, TRUE, "vidmem zbuffer", 0, FALSE,
6449 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
6452 /* Colorfill on YUV surfaces always returns DD_OK, but the content is
6453 * different afterwards. DX9+ GPUs set one of the two luminance values
6454 * in each block, but AMD and Nvidia GPUs disagree on which luminance
6455 * value they set. r200 (dx8) just sets the entire block to the clear
6456 * value. */
6457 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
6458 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain YUY2", 0, FALSE,
6460 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
6461 {0}, {0}, {0}, {0}, {0}
6465 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
6466 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain UYVY", 0, FALSE,
6468 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
6469 {0}, {0}, {0}, {0}, {0}
6473 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY,
6474 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay YUY2", 0, FALSE,
6476 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
6477 {0}, {0}, {0}, {0}, {0}
6481 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY,
6482 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay UYVY", 0, FALSE,
6484 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
6485 {0}, {0}, {0}, {0}, {0}
6489 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
6490 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "vidmem texture DXT1", 0, FALSE,
6492 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
6493 {0}, {0}, {0}, {0}, {0}
6497 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
6498 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "sysmem texture DXT1", 0, FALSE,
6500 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
6501 {0}, {0}, {0}, {0}, {0}
6505 /* The testbot fills this with 0x00 instead of the blue channel. The sysmem
6506 * surface works, presumably because it is handled by the runtime instead of
6507 * the driver. */
6508 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
6509 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain P8", 0xefefefef, FALSE,
6511 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
6512 {8}, {0}, {0}, {0}, {0}
6516 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
6517 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain P8", 0xefefefef, TRUE,
6519 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
6520 {8}, {0}, {0}, {0}, {0}
6524 static const struct
6526 DWORD rop;
6527 const char *name;
6528 HRESULT hr;
6530 rops[] =
6532 {SRCCOPY, "SRCCOPY", DD_OK},
6533 {SRCPAINT, "SRCPAINT", DDERR_NORASTEROPHW},
6534 {SRCAND, "SRCAND", DDERR_NORASTEROPHW},
6535 {SRCINVERT, "SRCINVERT", DDERR_NORASTEROPHW},
6536 {SRCERASE, "SRCERASE", DDERR_NORASTEROPHW},
6537 {NOTSRCCOPY, "NOTSRCCOPY", DDERR_NORASTEROPHW},
6538 {NOTSRCERASE, "NOTSRCERASE", DDERR_NORASTEROPHW},
6539 {MERGECOPY, "MERGECOPY", DDERR_NORASTEROPHW},
6540 {MERGEPAINT, "MERGEPAINT", DDERR_NORASTEROPHW},
6541 {PATCOPY, "PATCOPY", DDERR_NORASTEROPHW},
6542 {PATPAINT, "PATPAINT", DDERR_NORASTEROPHW},
6543 {PATINVERT, "PATINVERT", DDERR_NORASTEROPHW},
6544 {DSTINVERT, "DSTINVERT", DDERR_NORASTEROPHW},
6545 {BLACKNESS, "BLACKNESS", DD_OK},
6546 {WHITENESS, "WHITENESS", DD_OK},
6547 {0xaa0029, "0xaa0029", DDERR_NORASTEROPHW} /* noop */
6550 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6551 0, 0, 640, 480, 0, 0, 0, 0);
6552 ddraw = create_ddraw();
6553 ok(!!ddraw, "Failed to create a ddraw object.\n");
6554 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
6556 skip("Failed to create a 3D device, skipping test.\n");
6557 DestroyWindow(window);
6558 IDirectDraw_Release(ddraw);
6559 return;
6562 hr = IDirectDraw_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
6563 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
6564 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
6565 num_fourcc_codes * sizeof(*fourcc_codes));
6566 if (!fourcc_codes)
6567 goto done;
6568 hr = IDirectDraw_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
6569 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
6570 for (i = 0; i < num_fourcc_codes; i++)
6572 if (fourcc_codes[i] == MAKEFOURCC('Y', 'U', 'Y', '2'))
6573 support_yuy2 = TRUE;
6574 else if (fourcc_codes[i] == MAKEFOURCC('U', 'Y', 'V', 'Y'))
6575 support_uyvy = TRUE;
6577 HeapFree(GetProcessHeap(), 0, fourcc_codes);
6579 memset(&hal_caps, 0, sizeof(hal_caps));
6580 hal_caps.dwSize = sizeof(hal_caps);
6581 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
6582 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6584 if ((!support_yuy2 && !support_uyvy) || !(hal_caps.dwCaps & DDCAPS_OVERLAY))
6585 skip("Overlays or some YUV formats not supported, skipping YUV colorfill tests.\n");
6587 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
6589 /* Some Windows drivers modify dwFillColor when it is used on P8 or FourCC formats. */
6590 memset(&fx, 0, sizeof(fx));
6591 fx.dwSize = sizeof(fx);
6592 U5(fx).dwFillColor = 0xdeadbeef;
6594 memset(&surface_desc, 0, sizeof(surface_desc));
6595 surface_desc.dwSize = sizeof(surface_desc);
6596 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6597 surface_desc.dwWidth = 64;
6598 surface_desc.dwHeight = 64;
6599 surface_desc.ddpfPixelFormat = tests[i].format;
6600 surface_desc.ddsCaps.dwCaps = tests[i].caps;
6602 if (tests[i].caps & DDSCAPS_TEXTURE)
6604 struct format_support_check check = {&tests[i].format, FALSE};
6605 hr = IDirect3DDevice_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
6606 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
6607 if (!check.supported)
6608 continue;
6611 if (tests[i].format.dwFourCC == MAKEFOURCC('Y','U','Y','2') && !support_yuy2)
6612 continue;
6613 if (tests[i].format.dwFourCC == MAKEFOURCC('U','Y','V','Y') && !support_uyvy)
6614 continue;
6615 if (tests[i].caps & DDSCAPS_OVERLAY && !(hal_caps.dwCaps & DDCAPS_OVERLAY))
6616 continue;
6618 if (tests[i].caps & DDSCAPS_ZBUFFER)
6620 surface_desc.dwFlags &= ~DDSD_PIXELFORMAT;
6621 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
6622 U2(surface_desc).dwZBufferBitDepth = get_device_z_depth(device);
6625 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6626 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, surface %s.\n", hr, tests[i].name);
6628 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6629 if (tests[i].format.dwFourCC)
6630 todo_wine ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
6631 hr, tests[i].colorfill_hr, tests[i].name);
6632 else
6633 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
6634 hr, tests[i].colorfill_hr, tests[i].name);
6636 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6637 if (tests[i].format.dwFourCC)
6638 todo_wine ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
6639 hr, tests[i].colorfill_hr, tests[i].name);
6640 else
6641 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
6642 hr, tests[i].colorfill_hr, tests[i].name);
6644 if (SUCCEEDED(hr) && tests[i].check_result)
6646 memset(&surface_desc, 0, sizeof(surface_desc));
6647 surface_desc.dwSize = sizeof(surface_desc);
6648 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
6649 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
6650 color = surface_desc.lpSurface;
6651 ok(*color == tests[i].result, "Got clear result 0x%08x, expected 0x%08x, surface %s.\n",
6652 *color, tests[i].result, tests[i].name);
6653 hr = IDirectDrawSurface_Unlock(surface, NULL);
6654 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
6657 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6658 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
6659 hr, tests[i].depthfill_hr, tests[i].name);
6660 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6661 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
6662 hr, tests[i].depthfill_hr, tests[i].name);
6664 U5(fx).dwFillColor = 0xdeadbeef;
6665 fx.dwROP = BLACKNESS;
6666 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
6667 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
6668 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
6669 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
6670 U5(fx).dwFillColor, tests[i].name);
6672 if (SUCCEEDED(hr) && tests[i].check_result)
6674 memset(&surface_desc, 0, sizeof(surface_desc));
6675 surface_desc.dwSize = sizeof(surface_desc);
6676 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
6677 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
6678 color = surface_desc.lpSurface;
6679 ok(*color == 0, "Got clear result 0x%08x, expected 0x00000000, surface %s.\n",
6680 *color, tests[i].name);
6681 hr = IDirectDrawSurface_Unlock(surface, NULL);
6682 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
6685 fx.dwROP = WHITENESS;
6686 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
6687 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
6688 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
6689 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
6690 U5(fx).dwFillColor, tests[i].name);
6692 if (SUCCEEDED(hr) && tests[i].check_result)
6694 memset(&surface_desc, 0, sizeof(surface_desc));
6695 surface_desc.dwSize = sizeof(surface_desc);
6696 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
6697 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
6698 color = surface_desc.lpSurface;
6699 /* WHITENESS sets the alpha channel to 0x00. Ignore this for now. */
6700 ok((*color & 0x00ffffff) == 0x00ffffff, "Got clear result 0x%08x, expected 0xffffffff, surface %s.\n",
6701 *color, tests[i].name);
6702 hr = IDirectDrawSurface_Unlock(surface, NULL);
6703 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
6706 IDirectDrawSurface_Release(surface);
6709 memset(&fx, 0, sizeof(fx));
6710 fx.dwSize = sizeof(fx);
6711 U5(fx).dwFillColor = 0xdeadbeef;
6712 fx.dwROP = WHITENESS;
6714 memset(&surface_desc, 0, sizeof(surface_desc));
6715 surface_desc.dwSize = sizeof(surface_desc);
6716 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6717 surface_desc.dwWidth = 64;
6718 surface_desc.dwHeight = 64;
6719 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
6720 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
6721 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
6722 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6723 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6724 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6725 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
6726 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6727 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6728 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
6729 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6731 /* No DDBLTFX. */
6732 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, NULL);
6733 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6734 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, NULL);
6735 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6737 /* Unused source rectangle. */
6738 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6739 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6740 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
6741 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6743 /* Unused source surface. */
6744 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6745 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6746 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
6747 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6748 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6749 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6750 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
6751 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6753 /* Inverted destination or source rectangle. */
6754 SetRect(&rect, 5, 7, 7, 5);
6755 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6756 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6757 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6758 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6759 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6760 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6761 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6762 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6763 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
6764 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6766 /* Negative rectangle. */
6767 SetRect(&rect, -1, -1, 5, 5);
6768 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6769 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6770 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6771 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6772 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6773 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6774 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6775 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6776 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
6777 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6779 /* Out of bounds rectangle. */
6780 SetRect(&rect, 0, 0, 65, 65);
6781 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6782 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6783 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
6784 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6786 /* Combine multiple flags. */
6787 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6788 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6789 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
6790 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6791 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
6792 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6794 for (i = 0; i < sizeof(rops) / sizeof(*rops); i++)
6796 fx.dwROP = rops[i].rop;
6797 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
6798 ok(hr == rops[i].hr, "Got unexpected hr %#x for rop %s.\n", hr, rops[i].name);
6801 IDirectDrawSurface_Release(surface2);
6802 IDirectDrawSurface_Release(surface);
6804 memset(&surface_desc, 0, sizeof(surface_desc));
6805 surface_desc.dwSize = sizeof(surface_desc);
6806 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_ZBUFFERBITDEPTH;
6807 surface_desc.dwWidth = 64;
6808 surface_desc.dwHeight = 64;
6809 U2(surface_desc).dwZBufferBitDepth = get_device_z_depth(device);
6810 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
6811 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6812 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6813 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
6814 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6816 /* No DDBLTFX. */
6817 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, NULL);
6818 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6820 /* Unused source rectangle. */
6821 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6822 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6824 /* Unused source surface. */
6825 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6826 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6827 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6828 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6830 /* Inverted destination or source rectangle. */
6831 SetRect(&rect, 5, 7, 7, 5);
6832 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6833 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6834 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6835 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6836 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6837 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6838 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6839 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6841 /* Negative rectangle. */
6842 SetRect(&rect, -1, -1, 5, 5);
6843 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6844 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6845 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6846 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
6847 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6848 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6849 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6850 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6852 /* Out of bounds rectangle. */
6853 SetRect(&rect, 0, 0, 65, 65);
6854 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6855 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
6857 /* Combine multiple flags. */
6858 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
6859 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6861 IDirectDrawSurface_Release(surface2);
6862 IDirectDrawSurface_Release(surface);
6864 done:
6865 IDirect3DDevice_Release(device);
6866 refcount = IDirectDraw_Release(ddraw);
6867 ok(refcount == 0, "Ddraw object not properly released, refcount %u.\n", refcount);
6868 DestroyWindow(window);
6871 START_TEST(ddraw1)
6873 IDirectDraw *ddraw;
6874 DEVMODEW current_mode;
6876 if (!(ddraw = create_ddraw()))
6878 skip("Failed to create a ddraw object, skipping tests.\n");
6879 return;
6881 IDirectDraw_Release(ddraw);
6883 memset(&current_mode, 0, sizeof(current_mode));
6884 current_mode.dmSize = sizeof(current_mode);
6885 ok(EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &current_mode), "Failed to get display mode.\n");
6886 registry_mode.dmSize = sizeof(registry_mode);
6887 ok(EnumDisplaySettingsW(NULL, ENUM_REGISTRY_SETTINGS, &registry_mode), "Failed to get display mode.\n");
6888 if (registry_mode.dmPelsWidth != current_mode.dmPelsWidth
6889 || registry_mode.dmPelsHeight != current_mode.dmPelsHeight)
6891 skip("Current mode does not match registry mode, skipping test.\n");
6892 return;
6895 test_coop_level_create_device_window();
6896 test_clipper_blt();
6897 test_coop_level_d3d_state();
6898 test_surface_interface_mismatch();
6899 test_coop_level_threaded();
6900 test_viewport();
6901 test_zenable();
6902 test_ck_rgba();
6903 test_ck_default();
6904 test_ck_complex();
6905 test_surface_qi();
6906 test_device_qi();
6907 test_wndproc();
6908 test_window_style();
6909 test_redundant_mode_set();
6910 test_coop_level_mode_set();
6911 test_coop_level_mode_set_multi();
6912 test_initialize();
6913 test_coop_level_surf_create();
6914 test_coop_level_multi_window();
6915 test_clear_rect_count();
6916 test_coop_level_activateapp();
6917 test_unsupported_formats();
6918 test_rt_caps();
6919 test_primary_caps();
6920 test_surface_lock();
6921 test_surface_discard();
6922 test_flip();
6923 test_sysmem_overlay();
6924 test_primary_palette();
6925 test_surface_attachment();
6926 test_pixel_format();
6927 test_create_surface_pitch();
6928 test_mipmap_lock();
6929 test_palette_complex();
6930 test_p8_rgb_blit();
6931 test_material();
6932 test_palette_gdi();
6933 test_palette_alpha();
6934 test_lost_device();
6935 test_surface_desc_lock();
6936 test_texturemapblend();
6937 test_viewport_clear_rect();
6938 test_color_fill();