ddraw/tests: Rewrite offscreen_test().
[wine.git] / dlls / ddraw / tests / ddraw1.c
blob67410935f7904b9c6d65d783d8a705e152e026e4
1 /*
2 * Copyright 2005 Antoine Chavasse (a.chavasse@gmail.com)
3 * Copyright 2008, 2011, 2012-2013 Stefan Dösinger for CodeWeavers
4 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define COBJMACROS
22 #include "wine/test.h"
23 #include "d3d.h"
25 static BOOL is_ddraw64 = sizeof(DWORD) != sizeof(DWORD *);
26 static DEVMODEW registry_mode;
28 struct create_window_thread_param
30 HWND window;
31 HANDLE window_created;
32 HANDLE destroy_window;
33 HANDLE thread;
36 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
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 c1 >>= 8; c2 >>= 8;
44 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
45 return TRUE;
48 static IDirectDrawSurface *create_overlay(IDirectDraw *ddraw,
49 unsigned int width, unsigned int height, DWORD format)
51 IDirectDrawSurface *surface;
52 DDSURFACEDESC desc;
54 memset(&desc, 0, sizeof(desc));
55 desc.dwSize = sizeof(desc);
56 desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
57 desc.dwWidth = width;
58 desc.dwHeight = height;
59 desc.ddsCaps.dwCaps = DDSCAPS_OVERLAY;
60 desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat);
61 desc.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
62 desc.ddpfPixelFormat.dwFourCC = format;
64 if (FAILED(IDirectDraw_CreateSurface(ddraw, &desc, &surface, NULL)))
65 return NULL;
66 return surface;
69 static DWORD WINAPI create_window_thread_proc(void *param)
71 struct create_window_thread_param *p = param;
72 DWORD res;
73 BOOL ret;
75 p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
76 0, 0, 640, 480, 0, 0, 0, 0);
77 ret = SetEvent(p->window_created);
78 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
80 for (;;)
82 MSG msg;
84 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
85 DispatchMessageA(&msg);
86 res = WaitForSingleObject(p->destroy_window, 100);
87 if (res == WAIT_OBJECT_0)
88 break;
89 if (res != WAIT_TIMEOUT)
91 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
92 break;
96 DestroyWindow(p->window);
98 return 0;
101 static void create_window_thread(struct create_window_thread_param *p)
103 DWORD res, tid;
105 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
106 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
107 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
108 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
109 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
110 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
111 res = WaitForSingleObject(p->window_created, INFINITE);
112 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
115 static void destroy_window_thread(struct create_window_thread_param *p)
117 SetEvent(p->destroy_window);
118 WaitForSingleObject(p->thread, INFINITE);
119 CloseHandle(p->destroy_window);
120 CloseHandle(p->window_created);
121 CloseHandle(p->thread);
124 static HRESULT set_display_mode(IDirectDraw *ddraw, DWORD width, DWORD height)
126 if (SUCCEEDED(IDirectDraw_SetDisplayMode(ddraw, width, height, 32)))
127 return DD_OK;
128 return IDirectDraw_SetDisplayMode(ddraw, width, height, 24);
131 static D3DCOLOR get_surface_color(IDirectDrawSurface *surface, UINT x, UINT y)
133 RECT rect = {x, y, x + 1, y + 1};
134 DDSURFACEDESC surface_desc;
135 D3DCOLOR color;
136 HRESULT hr;
138 memset(&surface_desc, 0, sizeof(surface_desc));
139 surface_desc.dwSize = sizeof(surface_desc);
141 hr = IDirectDrawSurface_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
142 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
143 if (FAILED(hr))
144 return 0xdeadbeef;
146 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
148 hr = IDirectDrawSurface_Unlock(surface, NULL);
149 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
151 return color;
154 static void emit_process_vertices(void **ptr, DWORD flags, WORD base_idx, DWORD vertex_count)
156 D3DINSTRUCTION *inst = *ptr;
157 D3DPROCESSVERTICES *pv = (D3DPROCESSVERTICES *)(inst + 1);
159 inst->bOpcode = D3DOP_PROCESSVERTICES;
160 inst->bSize = sizeof(*pv);
161 inst->wCount = 1;
163 pv->dwFlags = flags;
164 pv->wStart = base_idx;
165 pv->wDest = 0;
166 pv->dwCount = vertex_count;
167 pv->dwReserved = 0;
169 *ptr = pv + 1;
172 static void emit_set_ts(void **ptr, D3DTRANSFORMSTATETYPE state, DWORD value)
174 D3DINSTRUCTION *inst = *ptr;
175 D3DSTATE *ts = (D3DSTATE *)(inst + 1);
177 inst->bOpcode = D3DOP_STATETRANSFORM;
178 inst->bSize = sizeof(*ts);
179 inst->wCount = 1;
181 U1(*ts).dtstTransformStateType = state;
182 U2(*ts).dwArg[0] = value;
184 *ptr = ts + 1;
187 static void emit_set_ls(void **ptr, D3DLIGHTSTATETYPE state, DWORD value)
189 D3DINSTRUCTION *inst = *ptr;
190 D3DSTATE *ls = (D3DSTATE *)(inst + 1);
192 inst->bOpcode = D3DOP_STATELIGHT;
193 inst->bSize = sizeof(*ls);
194 inst->wCount = 1;
196 U1(*ls).dlstLightStateType = state;
197 U2(*ls).dwArg[0] = value;
199 *ptr = ls + 1;
202 static void emit_set_rs(void **ptr, D3DRENDERSTATETYPE state, DWORD value)
204 D3DINSTRUCTION *inst = *ptr;
205 D3DSTATE *rs = (D3DSTATE *)(inst + 1);
207 inst->bOpcode = D3DOP_STATERENDER;
208 inst->bSize = sizeof(*rs);
209 inst->wCount = 1;
211 U1(*rs).drstRenderStateType = state;
212 U2(*rs).dwArg[0] = value;
214 *ptr = rs + 1;
217 static void emit_tquad(void **ptr, WORD base_idx)
219 D3DINSTRUCTION *inst = *ptr;
220 D3DTRIANGLE *tri = (D3DTRIANGLE *)(inst + 1);
222 inst->bOpcode = D3DOP_TRIANGLE;
223 inst->bSize = sizeof(*tri);
224 inst->wCount = 2;
226 U1(*tri).v1 = base_idx;
227 U2(*tri).v2 = base_idx + 1;
228 U3(*tri).v3 = base_idx + 2;
229 tri->wFlags = D3DTRIFLAG_START;
230 ++tri;
232 U1(*tri).v1 = base_idx + 2;
233 U2(*tri).v2 = base_idx + 1;
234 U3(*tri).v3 = base_idx + 3;
235 tri->wFlags = D3DTRIFLAG_ODD;
236 ++tri;
238 *ptr = tri;
241 static void emit_tquad_tlist(void **ptr, WORD base_idx)
243 D3DINSTRUCTION *inst = *ptr;
244 D3DTRIANGLE *tri = (D3DTRIANGLE *)(inst + 1);
246 inst->bOpcode = D3DOP_TRIANGLE;
247 inst->bSize = sizeof(*tri);
248 inst->wCount = 2;
250 U1(*tri).v1 = base_idx;
251 U2(*tri).v2 = base_idx + 1;
252 U3(*tri).v3 = base_idx + 2;
253 tri->wFlags = D3DTRIFLAG_START;
254 ++tri;
256 U1(*tri).v1 = base_idx + 2;
257 U2(*tri).v2 = base_idx + 3;
258 U3(*tri).v3 = base_idx;
259 tri->wFlags = D3DTRIFLAG_START;
260 ++tri;
262 *ptr = tri;
265 static void emit_end(void **ptr)
267 D3DINSTRUCTION *inst = *ptr;
269 inst->bOpcode = D3DOP_EXIT;
270 inst->bSize = 0;
271 inst->wCount = 0;
273 *ptr = inst + 1;
276 static void set_execute_data(IDirect3DExecuteBuffer *execute_buffer, UINT vertex_count, UINT offset, UINT len)
278 D3DEXECUTEDATA exec_data;
279 HRESULT hr;
281 memset(&exec_data, 0, sizeof(exec_data));
282 exec_data.dwSize = sizeof(exec_data);
283 exec_data.dwVertexCount = vertex_count;
284 exec_data.dwInstructionOffset = offset;
285 exec_data.dwInstructionLength = len;
286 hr = IDirect3DExecuteBuffer_SetExecuteData(execute_buffer, &exec_data);
287 ok(SUCCEEDED(hr), "Failed to set execute data, hr %#x.\n", hr);
290 static DWORD get_device_z_depth(IDirect3DDevice *device)
292 DDSCAPS caps = {DDSCAPS_ZBUFFER};
293 IDirectDrawSurface *ds, *rt;
294 DDSURFACEDESC desc;
295 HRESULT hr;
297 if (FAILED(IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt)))
298 return 0;
300 hr = IDirectDrawSurface_GetAttachedSurface(rt, &caps, &ds);
301 IDirectDrawSurface_Release(rt);
302 if (FAILED(hr))
303 return 0;
305 desc.dwSize = sizeof(desc);
306 hr = IDirectDrawSurface_GetSurfaceDesc(ds, &desc);
307 IDirectDrawSurface_Release(ds);
308 if (FAILED(hr))
309 return 0;
311 return U2(desc).dwZBufferBitDepth;
314 static IDirectDraw *create_ddraw(void)
316 IDirectDraw *ddraw;
318 if (FAILED(DirectDrawCreate(NULL, &ddraw, NULL)))
319 return NULL;
321 return ddraw;
324 static IDirect3DDevice *create_device(IDirectDraw *ddraw, HWND window, DWORD coop_level)
326 static const DWORD z_depths[] = {32, 24, 16};
327 IDirectDrawSurface *surface, *ds;
328 IDirect3DDevice *device = NULL;
329 DDSURFACEDESC surface_desc;
330 unsigned int i;
331 HRESULT hr;
333 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, coop_level);
334 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
336 memset(&surface_desc, 0, sizeof(surface_desc));
337 surface_desc.dwSize = sizeof(surface_desc);
338 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
339 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
340 surface_desc.dwWidth = 640;
341 surface_desc.dwHeight = 480;
343 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
344 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
346 if (coop_level & DDSCL_NORMAL)
348 IDirectDrawClipper *clipper;
350 hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
351 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
352 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
353 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
354 hr = IDirectDrawSurface_SetClipper(surface, clipper);
355 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
356 IDirectDrawClipper_Release(clipper);
359 /* We used to use EnumDevices() for this, but it seems
360 * D3DDEVICEDESC.dwDeviceZBufferBitDepth only has a very casual
361 * relationship with reality. */
362 for (i = 0; i < sizeof(z_depths) / sizeof(*z_depths); ++i)
364 memset(&surface_desc, 0, sizeof(surface_desc));
365 surface_desc.dwSize = sizeof(surface_desc);
366 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
367 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
368 U2(surface_desc).dwZBufferBitDepth = z_depths[i];
369 surface_desc.dwWidth = 640;
370 surface_desc.dwHeight = 480;
371 if (FAILED(IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL)))
372 continue;
374 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
375 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
376 IDirectDrawSurface_Release(ds);
377 if (FAILED(hr))
378 continue;
380 if (SUCCEEDED(IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device)))
381 break;
383 IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
386 IDirectDrawSurface_Release(surface);
387 return device;
390 static IDirect3DViewport *create_viewport(IDirect3DDevice *device, UINT x, UINT y, UINT w, UINT h)
392 IDirect3DViewport *viewport;
393 D3DVIEWPORT vp;
394 IDirect3D *d3d;
395 HRESULT hr;
397 hr = IDirect3DDevice_GetDirect3D(device, &d3d);
398 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
399 hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
400 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
401 hr = IDirect3DDevice_AddViewport(device, viewport);
402 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
403 memset(&vp, 0, sizeof(vp));
404 vp.dwSize = sizeof(vp);
405 vp.dwX = x;
406 vp.dwY = y;
407 vp.dwWidth = w;
408 vp.dwHeight = h;
409 vp.dvScaleX = (float)w / 2.0f;
410 vp.dvScaleY = (float)h / 2.0f;
411 vp.dvMaxX = 1.0f;
412 vp.dvMaxY = 1.0f;
413 vp.dvMinZ = 0.0f;
414 vp.dvMaxZ = 1.0f;
415 hr = IDirect3DViewport_SetViewport(viewport, &vp);
416 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
417 IDirect3D_Release(d3d);
419 return viewport;
422 static void viewport_set_background(IDirect3DDevice *device, IDirect3DViewport *viewport,
423 IDirect3DMaterial *material)
425 D3DMATERIALHANDLE material_handle;
426 HRESULT hr;
428 hr = IDirect3DMaterial2_GetHandle(material, device, &material_handle);
429 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
430 hr = IDirect3DViewport2_SetBackground(viewport, material_handle);
431 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
434 static void destroy_viewport(IDirect3DDevice *device, IDirect3DViewport *viewport)
436 HRESULT hr;
438 hr = IDirect3DDevice_DeleteViewport(device, viewport);
439 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
440 IDirect3DViewport_Release(viewport);
443 static IDirect3DMaterial *create_material(IDirect3DDevice *device, D3DMATERIAL *mat)
445 IDirect3DMaterial *material;
446 IDirect3D *d3d;
447 HRESULT hr;
449 hr = IDirect3DDevice_GetDirect3D(device, &d3d);
450 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
451 hr = IDirect3D_CreateMaterial(d3d, &material, NULL);
452 ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
453 hr = IDirect3DMaterial_SetMaterial(material, mat);
454 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
455 IDirect3D_Release(d3d);
457 return material;
460 static IDirect3DMaterial *create_diffuse_material(IDirect3DDevice *device, float r, float g, float b, float a)
462 D3DMATERIAL mat;
464 memset(&mat, 0, sizeof(mat));
465 mat.dwSize = sizeof(mat);
466 U1(U(mat).diffuse).r = r;
467 U2(U(mat).diffuse).g = g;
468 U3(U(mat).diffuse).b = b;
469 U4(U(mat).diffuse).a = a;
471 return create_material(device, &mat);
474 static IDirect3DMaterial *create_emissive_material(IDirect3DDevice *device, float r, float g, float b, float a)
476 D3DMATERIAL mat;
478 memset(&mat, 0, sizeof(mat));
479 mat.dwSize = sizeof(mat);
480 U1(U3(mat).emissive).r = r;
481 U2(U3(mat).emissive).g = g;
482 U3(U3(mat).emissive).b = b;
483 U4(U3(mat).emissive).a = a;
485 return create_material(device, &mat);
488 static void destroy_material(IDirect3DMaterial *material)
490 IDirect3DMaterial_Release(material);
493 struct message
495 UINT message;
496 BOOL check_wparam;
497 WPARAM expect_wparam;
500 static const struct message *expect_messages;
502 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
504 if (expect_messages && message == expect_messages->message)
506 if (expect_messages->check_wparam)
507 ok (wparam == expect_messages->expect_wparam,
508 "Got unexpected wparam %lx for message %x, expected %lx.\n",
509 wparam, message, expect_messages->expect_wparam);
511 ++expect_messages;
514 return DefWindowProcA(hwnd, message, wparam, lparam);
517 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
518 * interface. This prevents subsequent SetCooperativeLevel() calls on a
519 * different window from failing with DDERR_HWNDALREADYSET. */
520 static void fix_wndproc(HWND window, LONG_PTR proc)
522 IDirectDraw *ddraw;
523 HRESULT hr;
525 if (!(ddraw = create_ddraw()))
526 return;
528 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
529 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
530 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
531 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
532 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
534 IDirectDraw_Release(ddraw);
537 static HRESULT CALLBACK restore_callback(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
539 HRESULT hr = IDirectDrawSurface_Restore(surface);
540 ok(SUCCEEDED(hr) || hr == DDERR_IMPLICITLYCREATED, "Failed to restore surface, hr %#x.\n", hr);
541 IDirectDrawSurface_Release(surface);
543 return DDENUMRET_OK;
546 static HRESULT restore_surfaces(IDirectDraw *ddraw)
548 return IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
549 NULL, NULL, restore_callback);
552 static void test_coop_level_create_device_window(void)
554 HWND focus_window, device_window;
555 IDirectDraw *ddraw;
556 HRESULT hr;
558 focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
559 0, 0, 640, 480, 0, 0, 0, 0);
560 ddraw = create_ddraw();
561 ok(!!ddraw, "Failed to create a ddraw object.\n");
563 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
564 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
565 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
566 ok(!device_window, "Unexpected device window found.\n");
567 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
568 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
569 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
570 ok(!device_window, "Unexpected device window found.\n");
571 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
572 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
573 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
574 ok(!device_window, "Unexpected device window found.\n");
575 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
576 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
577 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
578 ok(!device_window, "Unexpected device window found.\n");
579 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
580 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
581 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
582 ok(!device_window, "Unexpected device window found.\n");
584 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
585 if (broken(hr == DDERR_INVALIDPARAMS))
587 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
588 IDirectDraw_Release(ddraw);
589 DestroyWindow(focus_window);
590 return;
593 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
594 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
595 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
596 ok(!device_window, "Unexpected device window found.\n");
597 hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
598 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
599 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
600 ok(!device_window, "Unexpected device window found.\n");
602 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
603 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
604 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
605 ok(!device_window, "Unexpected device window found.\n");
606 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
607 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
608 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
609 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
610 ok(!!device_window, "Device window not found.\n");
612 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
613 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
614 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
615 ok(!device_window, "Unexpected device window found.\n");
616 hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
617 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
618 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
619 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
620 ok(!!device_window, "Device window not found.\n");
622 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
623 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
624 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
625 ok(!device_window, "Unexpected device window found.\n");
626 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
627 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
628 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
629 ok(!device_window, "Unexpected device window found.\n");
630 hr = IDirectDraw_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
631 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
632 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
633 ok(!device_window, "Unexpected device window found.\n");
634 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
635 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
636 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
637 ok(!!device_window, "Device window not found.\n");
639 IDirectDraw_Release(ddraw);
640 DestroyWindow(focus_window);
643 static void test_clipper_blt(void)
645 IDirectDrawSurface *src_surface, *dst_surface;
646 RECT client_rect, src_rect;
647 IDirectDrawClipper *clipper;
648 DDSURFACEDESC surface_desc;
649 unsigned int i, j, x, y;
650 IDirectDraw *ddraw;
651 RGNDATA *rgn_data;
652 D3DCOLOR color;
653 ULONG refcount;
654 HRGN r1, r2;
655 HWND window;
656 DDBLTFX fx;
657 HRESULT hr;
658 DWORD *ptr;
659 DWORD ret;
661 static const DWORD src_data[] =
663 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
664 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
665 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
667 static const D3DCOLOR expected1[] =
669 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
670 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
671 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
672 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
674 /* Nvidia on Windows seems to have an off-by-one error
675 * when processing source rectangles. Our left = 1 and
676 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
677 * read as well, but only for the edge pixels on the
678 * output image. The bug happens on the y axis as well,
679 * but we only read one row there, and all source rows
680 * contain the same data. This bug is not dependent on
681 * the presence of a clipper. */
682 static const D3DCOLOR expected1_broken[] =
684 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
685 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
686 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
687 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
689 static const D3DCOLOR expected2[] =
691 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
692 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
693 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
694 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
697 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
698 10, 10, 640, 480, 0, 0, 0, 0);
699 ShowWindow(window, SW_SHOW);
700 ddraw = create_ddraw();
701 ok(!!ddraw, "Failed to create a ddraw object.\n");
703 ret = GetClientRect(window, &client_rect);
704 ok(ret, "Failed to get client rect.\n");
705 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
706 ok(ret, "Failed to map client rect.\n");
708 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
709 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
711 hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
712 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
713 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
714 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
715 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
716 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
717 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
718 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
719 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
720 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
721 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
722 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
723 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
724 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
725 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
726 "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
727 rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
728 rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
729 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
730 HeapFree(GetProcessHeap(), 0, rgn_data);
732 r1 = CreateRectRgn(0, 0, 320, 240);
733 ok(!!r1, "Failed to create region.\n");
734 r2 = CreateRectRgn(320, 240, 640, 480);
735 ok(!!r2, "Failed to create region.\n");
736 CombineRgn(r1, r1, r2, RGN_OR);
737 ret = GetRegionData(r1, 0, NULL);
738 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
739 ret = GetRegionData(r1, ret, rgn_data);
740 ok(!!ret, "Failed to get region data.\n");
742 DeleteObject(r2);
743 DeleteObject(r1);
745 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
746 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
747 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
748 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
749 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
750 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
752 HeapFree(GetProcessHeap(), 0, rgn_data);
754 memset(&surface_desc, 0, sizeof(surface_desc));
755 surface_desc.dwSize = sizeof(surface_desc);
756 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
757 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
758 surface_desc.dwWidth = 640;
759 surface_desc.dwHeight = 480;
760 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
761 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
762 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
763 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
764 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
765 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
767 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
768 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
769 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
770 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
772 memset(&fx, 0, sizeof(fx));
773 fx.dwSize = sizeof(fx);
774 hr = IDirectDrawSurface_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
775 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
776 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
777 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
779 hr = IDirectDrawSurface_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
780 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
781 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
782 ptr = surface_desc.lpSurface;
783 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
784 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
785 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
786 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
787 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
789 hr = IDirectDrawSurface_SetClipper(dst_surface, clipper);
790 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
792 SetRect(&src_rect, 1, 1, 5, 2);
793 hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
794 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
795 for (i = 0; i < 4; ++i)
797 for (j = 0; j < 4; ++j)
799 x = 80 * ((2 * j) + 1);
800 y = 60 * ((2 * i) + 1);
801 color = get_surface_color(dst_surface, x, y);
802 ok(compare_color(color, expected1[i * 4 + j], 1)
803 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
804 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
808 U5(fx).dwFillColor = 0xff0000ff;
809 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
810 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
811 for (i = 0; i < 4; ++i)
813 for (j = 0; j < 4; ++j)
815 x = 80 * ((2 * j) + 1);
816 y = 60 * ((2 * i) + 1);
817 color = get_surface_color(dst_surface, x, y);
818 ok(compare_color(color, expected2[i * 4 + j], 1),
819 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
823 hr = IDirectDrawSurface_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
824 ok(hr == DDERR_BLTFASTCANTCLIP || broken(hr == E_NOTIMPL /* NT4 */), "Got unexpected hr %#x.\n", hr);
826 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
827 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
828 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
829 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
830 DestroyWindow(window);
831 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
832 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
833 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
834 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
835 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
836 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
837 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
838 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
839 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
840 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
841 hr = IDirectDrawSurface_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
842 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
844 IDirectDrawSurface_Release(dst_surface);
845 IDirectDrawSurface_Release(src_surface);
846 refcount = IDirectDrawClipper_Release(clipper);
847 ok(!refcount, "Clipper has %u references left.\n", refcount);
848 IDirectDraw_Release(ddraw);
851 static void test_coop_level_d3d_state(void)
853 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
854 IDirectDrawSurface *rt, *surface;
855 IDirect3DMaterial *background;
856 IDirect3DViewport *viewport;
857 IDirect3DDevice *device;
858 D3DMATERIAL material;
859 IDirectDraw *ddraw;
860 D3DCOLOR color;
861 HWND window;
862 HRESULT hr;
864 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
865 0, 0, 640, 480, 0, 0, 0, 0);
866 ddraw = create_ddraw();
867 ok(!!ddraw, "Failed to create a ddraw object.\n");
868 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
870 skip("Failed to create a 3D device, skipping test.\n");
871 IDirectDraw_Release(ddraw);
872 DestroyWindow(window);
873 return;
876 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
877 viewport = create_viewport(device, 0, 0, 640, 480);
878 viewport_set_background(device, viewport, background);
880 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
881 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
882 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
883 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
884 color = get_surface_color(rt, 320, 240);
885 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
887 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
888 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
889 hr = IDirectDrawSurface_IsLost(rt);
890 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
891 hr = restore_surfaces(ddraw);
892 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
894 memset(&material, 0, sizeof(material));
895 material.dwSize = sizeof(material);
896 U1(U(material).diffuse).r = 0.0f;
897 U2(U(material).diffuse).g = 1.0f;
898 U3(U(material).diffuse).b = 0.0f;
899 U4(U(material).diffuse).a = 1.0f;
900 hr = IDirect3DMaterial_SetMaterial(background, &material);
901 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
903 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&surface);
904 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
905 ok(surface == rt, "Got unexpected surface %p.\n", surface);
906 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
907 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
908 color = get_surface_color(rt, 320, 240);
909 ok(compare_color(color, 0x0000ff00, 1) || broken(compare_color(color, 0x00000000, 1)),
910 "Got unexpected color 0x%08x.\n", color);
912 destroy_viewport(device, viewport);
913 destroy_material(background);
914 IDirectDrawSurface_Release(surface);
915 IDirectDrawSurface_Release(rt);
916 IDirect3DDevice_Release(device);
917 IDirectDraw_Release(ddraw);
918 DestroyWindow(window);
921 static void test_surface_interface_mismatch(void)
923 IDirectDraw *ddraw = NULL;
924 IDirectDrawSurface *surface = NULL, *ds;
925 IDirectDrawSurface3 *surface3 = NULL;
926 IDirect3DDevice *device = NULL;
927 IDirect3DViewport *viewport = NULL;
928 IDirect3DMaterial *background = NULL;
929 DDSURFACEDESC surface_desc;
930 DWORD z_depth = 0;
931 ULONG refcount;
932 HRESULT hr;
933 D3DCOLOR color;
934 HWND window;
935 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
937 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
938 0, 0, 640, 480, 0, 0, 0, 0);
939 ddraw = create_ddraw();
940 ok(!!ddraw, "Failed to create a ddraw object.\n");
941 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
943 skip("Failed to create a 3D device, skipping test.\n");
944 IDirectDraw_Release(ddraw);
945 DestroyWindow(window);
946 return;
948 z_depth = get_device_z_depth(device);
949 ok(!!z_depth, "Failed to get device z depth.\n");
950 IDirect3DDevice_Release(device);
951 device = NULL;
953 memset(&surface_desc, 0, sizeof(surface_desc));
954 surface_desc.dwSize = sizeof(surface_desc);
955 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
956 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
957 surface_desc.dwWidth = 640;
958 surface_desc.dwHeight = 480;
960 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
961 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
963 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
964 if (FAILED(hr))
966 skip("Failed to get the IDirectDrawSurface3 interface, skipping test.\n");
967 goto cleanup;
970 memset(&surface_desc, 0, sizeof(surface_desc));
971 surface_desc.dwSize = sizeof(surface_desc);
972 surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
973 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
974 U2(surface_desc).dwZBufferBitDepth = z_depth;
975 surface_desc.dwWidth = 640;
976 surface_desc.dwHeight = 480;
977 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
978 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
979 if (FAILED(hr))
980 goto cleanup;
982 /* Using a different surface interface version still works */
983 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
984 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
985 refcount = IDirectDrawSurface_Release(ds);
986 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
987 if (FAILED(hr))
988 goto cleanup;
990 /* Here too */
991 hr = IDirectDrawSurface3_QueryInterface(surface3, &IID_IDirect3DHALDevice, (void **)&device);
992 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
993 if (FAILED(hr))
994 goto cleanup;
996 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
997 viewport = create_viewport(device, 0, 0, 640, 480);
998 viewport_set_background(device, viewport, background);
1000 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1001 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
1002 color = get_surface_color(surface, 320, 240);
1003 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
1005 cleanup:
1006 if (viewport)
1007 destroy_viewport(device, viewport);
1008 if (background)
1009 destroy_material(background);
1010 if (surface3) IDirectDrawSurface3_Release(surface3);
1011 if (surface) IDirectDrawSurface_Release(surface);
1012 if (device) IDirect3DDevice_Release(device);
1013 if (ddraw) IDirectDraw_Release(ddraw);
1014 DestroyWindow(window);
1017 static void test_coop_level_threaded(void)
1019 struct create_window_thread_param p;
1020 IDirectDraw *ddraw;
1021 HRESULT hr;
1023 ddraw = create_ddraw();
1024 ok(!!ddraw, "Failed to create a ddraw object.\n");
1025 create_window_thread(&p);
1027 hr = IDirectDraw_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1028 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1030 IDirectDraw_Release(ddraw);
1031 destroy_window_thread(&p);
1034 static ULONG get_refcount(IUnknown *test_iface)
1036 IUnknown_AddRef(test_iface);
1037 return IUnknown_Release(test_iface);
1040 static void test_viewport(void)
1042 IDirectDraw *ddraw;
1043 IDirect3D *d3d;
1044 HRESULT hr;
1045 ULONG ref;
1046 IDirect3DViewport *viewport, *another_vp;
1047 IDirect3DViewport2 *viewport2;
1048 IDirect3DViewport3 *viewport3;
1049 IDirectDrawGammaControl *gamma;
1050 IUnknown *unknown;
1051 IDirect3DDevice *device;
1052 HWND window;
1054 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1055 0, 0, 640, 480, 0, 0, 0, 0);
1056 ddraw = create_ddraw();
1057 ok(!!ddraw, "Failed to create a ddraw object.\n");
1058 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1060 skip("Failed to create a 3D device, skipping test.\n");
1061 IDirectDraw_Release(ddraw);
1062 DestroyWindow(window);
1063 return;
1066 hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
1067 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1068 ref = get_refcount((IUnknown *) d3d);
1069 ok(ref == 2, "IDirect3D refcount is %d\n", ref);
1071 hr = IDirect3D_CreateViewport(d3d, &viewport, NULL);
1072 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1073 ref = get_refcount((IUnknown *)viewport);
1074 ok(ref == 1, "Initial IDirect3DViewport refcount is %u\n", ref);
1075 ref = get_refcount((IUnknown *)d3d);
1076 ok(ref == 2, "IDirect3D refcount is %u\n", ref);
1078 /* E_FAIL return values are returned by Winetestbot Windows NT machines. While not supporting
1079 * newer interfaces is legitimate for old ddraw versions, E_FAIL violates Microsoft's rules
1080 * for QueryInterface, hence the broken() */
1081 gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1082 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirectDrawGammaControl, (void **)&gamma);
1083 ok(hr == E_NOINTERFACE || broken(hr == E_FAIL), "Got unexpected hr %#x.\n", hr);
1084 ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1085 if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1086 /* NULL iid: Segfaults */
1088 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport2, (void **)&viewport2);
1089 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
1090 "Failed to QI IDirect3DViewport2, hr %#x.\n", hr);
1091 if (viewport2)
1093 ref = get_refcount((IUnknown *)viewport);
1094 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1095 ref = get_refcount((IUnknown *)viewport2);
1096 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1097 IDirect3DViewport2_Release(viewport2);
1098 viewport2 = NULL;
1101 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IDirect3DViewport3, (void **)&viewport3);
1102 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE || broken(hr == E_FAIL),
1103 "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1104 if (viewport3)
1106 ref = get_refcount((IUnknown *)viewport);
1107 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1108 ref = get_refcount((IUnknown *)viewport3);
1109 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1110 IDirect3DViewport3_Release(viewport3);
1113 hr = IDirect3DViewport_QueryInterface(viewport, &IID_IUnknown, (void **)&unknown);
1114 ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1115 if (unknown)
1117 ref = get_refcount((IUnknown *)viewport);
1118 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1119 ref = get_refcount(unknown);
1120 ok(ref == 2, "IUnknown refcount is %u\n", ref);
1121 IUnknown_Release(unknown);
1124 /* AddViewport(NULL): Segfault */
1125 hr = IDirect3DDevice_DeleteViewport(device, NULL);
1126 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1128 hr = IDirect3D_CreateViewport(d3d, &another_vp, NULL);
1129 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1131 hr = IDirect3DDevice_AddViewport(device, viewport);
1132 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1133 ref = get_refcount((IUnknown *) viewport);
1134 ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
1135 hr = IDirect3DDevice_AddViewport(device, another_vp);
1136 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1137 ref = get_refcount((IUnknown *) another_vp);
1138 ok(ref == 2, "IDirect3DViewport refcount is %d\n", ref);
1140 hr = IDirect3DDevice_DeleteViewport(device, another_vp);
1141 ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1142 ref = get_refcount((IUnknown *) another_vp);
1143 ok(ref == 1, "IDirect3DViewport refcount is %d\n", ref);
1145 IDirect3DDevice_Release(device);
1146 ref = get_refcount((IUnknown *) viewport);
1147 ok(ref == 1, "IDirect3DViewport refcount is %d\n", ref);
1149 IDirect3DViewport_Release(another_vp);
1150 IDirect3D_Release(d3d);
1151 IDirect3DViewport_Release(viewport);
1152 DestroyWindow(window);
1153 IDirectDraw_Release(ddraw);
1156 static void test_zenable(void)
1158 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1159 static D3DTLVERTEX tquad[] =
1161 {{ 0.0f}, {480.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1162 {{ 0.0f}, { 0.0f}, {-0.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1163 {{640.0f}, {480.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1164 {{640.0f}, { 0.0f}, { 1.5f}, {1.0f}, {0xff00ff00}, {0x00000000}, {0.0f}, {0.0f}},
1166 IDirect3DExecuteBuffer *execute_buffer;
1167 D3DEXECUTEBUFFERDESC exec_desc;
1168 IDirect3DMaterial *background;
1169 IDirect3DViewport *viewport;
1170 IDirect3DDevice *device;
1171 IDirectDrawSurface *rt;
1172 IDirectDraw *ddraw;
1173 UINT inst_length;
1174 D3DCOLOR color;
1175 HWND window;
1176 HRESULT hr;
1177 UINT x, y;
1178 UINT i, j;
1179 void *ptr;
1181 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1182 0, 0, 640, 480, 0, 0, 0, 0);
1183 ddraw = create_ddraw();
1184 ok(!!ddraw, "Failed to create a ddraw object.\n");
1185 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1187 skip("Failed to create a 3D device, skipping test.\n");
1188 IDirectDraw_Release(ddraw);
1189 DestroyWindow(window);
1190 return;
1193 background = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
1194 viewport = create_viewport(device, 0, 0, 640, 480);
1195 viewport_set_background(device, viewport, background);
1197 memset(&exec_desc, 0, sizeof(exec_desc));
1198 exec_desc.dwSize = sizeof(exec_desc);
1199 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1200 exec_desc.dwBufferSize = 1024;
1201 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1203 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1204 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1205 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1206 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1207 memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1208 ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1209 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1210 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1211 emit_tquad(&ptr, 0);
1212 emit_end(&ptr);
1213 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
1214 inst_length -= sizeof(tquad);
1215 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1216 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1218 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1219 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1220 hr = IDirect3DDevice_BeginScene(device);
1221 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1222 set_execute_data(execute_buffer, 4, sizeof(tquad), inst_length);
1223 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1224 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1225 hr = IDirect3DDevice_EndScene(device);
1226 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1228 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1229 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1230 for (i = 0; i < 4; ++i)
1232 for (j = 0; j < 4; ++j)
1234 x = 80 * ((2 * j) + 1);
1235 y = 60 * ((2 * i) + 1);
1236 color = get_surface_color(rt, x, y);
1237 ok(compare_color(color, 0x0000ff00, 1),
1238 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1241 IDirectDrawSurface_Release(rt);
1243 destroy_viewport(device, viewport);
1244 IDirect3DExecuteBuffer_Release(execute_buffer);
1245 destroy_material(background);
1246 IDirect3DDevice_Release(device);
1247 IDirectDraw_Release(ddraw);
1248 DestroyWindow(window);
1251 static void test_ck_rgba(void)
1253 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1254 static D3DTLVERTEX tquad[] =
1256 {{ 0.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1257 {{ 0.0f}, { 0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1258 {{640.0f}, {480.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1259 {{640.0f}, { 0.0f}, {0.25f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1260 {{ 0.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1261 {{ 0.0f}, { 0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1262 {{640.0f}, {480.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1263 {{640.0f}, { 0.0f}, {0.75f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1265 /* Supposedly there was no D3DRENDERSTATE_COLORKEYENABLE in D3D < 5.
1266 * Maybe the WARP driver on Windows 8 ignores setting it via the older
1267 * device interface but it's buggy in that the internal state is not
1268 * initialized, or possibly toggling D3DRENDERSTATE_COLORKEYENABLE /
1269 * D3DRENDERSTATE_ALPHABLENDENABLE has unintended side effects.
1270 * Checking the W8 test results it seems like test 1 fails most of the time
1271 * and test 0 fails very rarely. */
1272 static const struct
1274 D3DCOLOR fill_color;
1275 BOOL color_key;
1276 BOOL blend;
1277 D3DCOLOR result1, result1_r200, result1_warp;
1278 D3DCOLOR result2, result2_r200, result2_warp;
1280 tests[] =
1282 /* r200 on Windows doesn't check the alpha component when applying the color
1283 * key, so the key matches on every texel. */
1284 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x00ff0000, 0x0000ff00,
1285 0x000000ff, 0x000000ff, 0x0000ff00},
1286 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x00ff0000, 0x0000ff00,
1287 0x000000ff, 0x000000ff, 0x0000ff00},
1288 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00, 0x0000ff00,
1289 0x0000ff00, 0x0000ff00, 0x0000ff00},
1290 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00,
1291 0x0000ff00, 0x0000ff00, 0x0000ff00},
1292 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00ff0000, 0x00807f00,
1293 0x00807f00, 0x000000ff, 0x00807f00},
1294 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x00ff0000, 0x0000ff00,
1295 0x0000ff00, 0x000000ff, 0x0000ff00},
1296 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00, 0x00807f00,
1297 0x00807f00, 0x00807f00, 0x00807f00},
1298 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00, 0x0000ff00,
1299 0x0000ff00, 0x0000ff00, 0x0000ff00},
1302 IDirect3DExecuteBuffer *execute_buffer;
1303 D3DTEXTUREHANDLE texture_handle;
1304 D3DEXECUTEBUFFERDESC exec_desc;
1305 IDirect3DMaterial *background;
1306 IDirectDrawSurface *surface;
1307 IDirect3DViewport *viewport;
1308 DDSURFACEDESC surface_desc;
1309 IDirect3DTexture *texture;
1310 IDirect3DDevice *device;
1311 IDirectDrawSurface *rt;
1312 IDirectDraw *ddraw;
1313 D3DCOLOR color;
1314 HWND window;
1315 DDBLTFX fx;
1316 HRESULT hr;
1317 UINT i;
1319 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1320 0, 0, 640, 480, 0, 0, 0, 0);
1321 ddraw = create_ddraw();
1322 ok(!!ddraw, "Failed to create a ddraw object.\n");
1323 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1325 skip("Failed to create a 3D device, skipping test.\n");
1326 IDirectDraw_Release(ddraw);
1327 DestroyWindow(window);
1328 return;
1331 background = create_diffuse_material(device, 1.0, 0.0f, 0.0f, 1.0f);
1332 viewport = create_viewport(device, 0, 0, 640, 480);
1333 viewport_set_background(device, viewport, background);
1335 memset(&surface_desc, 0, sizeof(surface_desc));
1336 surface_desc.dwSize = sizeof(surface_desc);
1337 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1338 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1339 surface_desc.dwWidth = 256;
1340 surface_desc.dwHeight = 256;
1341 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1342 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1343 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1344 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1345 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1346 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1347 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1348 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1349 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1350 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1351 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1352 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1353 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1354 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1355 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1356 IDirect3DTexture_Release(texture);
1358 memset(&exec_desc, 0, sizeof(exec_desc));
1359 exec_desc.dwSize = sizeof(exec_desc);
1360 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1361 exec_desc.dwBufferSize = 1024;
1362 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1363 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1364 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1366 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1367 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1369 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1371 UINT draw1_len, draw2_len;
1372 void *ptr;
1374 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1375 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1376 memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1377 ptr = ((BYTE *)exec_desc.lpData) + sizeof(tquad);
1378 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1379 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1380 emit_set_rs(&ptr, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1381 emit_set_rs(&ptr, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1382 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1383 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1384 emit_tquad(&ptr, 0);
1385 emit_end(&ptr);
1386 draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - sizeof(tquad);
1387 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 4, 4);
1388 emit_tquad(&ptr, 0);
1389 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1390 emit_end(&ptr);
1391 draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_len;
1392 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1393 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1395 memset(&fx, 0, sizeof(fx));
1396 fx.dwSize = sizeof(fx);
1397 U5(fx).dwFillColor = tests[i].fill_color;
1398 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1399 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1401 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
1402 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1403 hr = IDirect3DDevice_BeginScene(device);
1404 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1405 set_execute_data(execute_buffer, 8, sizeof(tquad), draw1_len);
1406 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1407 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1408 hr = IDirect3DDevice_EndScene(device);
1409 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1411 color = get_surface_color(rt, 320, 240);
1412 ok(compare_color(color, tests[i].result1, 1)
1413 || broken(compare_color(color, tests[i].result1_r200, 1))
1414 || broken(compare_color(color, tests[i].result1_warp, 1)),
1415 "Got unexpected color 0x%08x for test %u.\n", color, i);
1417 U5(fx).dwFillColor = 0xff0000ff;
1418 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1419 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1421 hr = IDirect3DDevice_BeginScene(device);
1422 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1423 set_execute_data(execute_buffer, 8, sizeof(tquad) + draw1_len, draw2_len);
1424 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1425 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1426 hr = IDirect3DDevice_EndScene(device);
1427 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1429 /* This tests that fragments that are masked out by the color key are
1430 * discarded, instead of just fully transparent. */
1431 color = get_surface_color(rt, 320, 240);
1432 ok(compare_color(color, tests[i].result2, 1)
1433 || broken(compare_color(color, tests[i].result2_r200, 1))
1434 || broken(compare_color(color, tests[i].result2_warp, 1)),
1435 "Got unexpected color 0x%08x for test %u.\n", color, i);
1438 IDirectDrawSurface_Release(rt);
1439 IDirect3DExecuteBuffer_Release(execute_buffer);
1440 IDirectDrawSurface_Release(surface);
1441 destroy_viewport(device, viewport);
1442 destroy_material(background);
1443 IDirect3DDevice_Release(device);
1444 IDirectDraw_Release(ddraw);
1445 DestroyWindow(window);
1448 static void test_ck_default(void)
1450 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1451 static D3DTLVERTEX tquad[] =
1453 {{ 0.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {0.0f}},
1454 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {0.0f}, {1.0f}},
1455 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {0.0f}},
1456 {{640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0x00000000}, {1.0f}, {1.0f}},
1458 IDirect3DExecuteBuffer *execute_buffer;
1459 IDirectDrawSurface *surface, *rt;
1460 D3DTEXTUREHANDLE texture_handle;
1461 D3DEXECUTEBUFFERDESC exec_desc;
1462 IDirect3DMaterial *background;
1463 UINT draw1_offset, draw1_len;
1464 UINT draw2_offset, draw2_len;
1465 UINT draw3_offset, draw3_len;
1466 UINT draw4_offset, draw4_len;
1467 IDirect3DViewport *viewport;
1468 DDSURFACEDESC surface_desc;
1469 IDirect3DTexture *texture;
1470 IDirect3DDevice *device;
1471 IDirectDraw *ddraw;
1472 D3DCOLOR color;
1473 HWND window;
1474 DDBLTFX fx;
1475 HRESULT hr;
1476 void *ptr;
1478 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1479 0, 0, 640, 480, 0, 0, 0, 0);
1480 ddraw = create_ddraw();
1481 ok(!!ddraw, "Failed to create a ddraw object.\n");
1482 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1484 skip("Failed to create a 3D device, skipping test.\n");
1485 IDirectDraw_Release(ddraw);
1486 DestroyWindow(window);
1487 return;
1490 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
1491 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1493 background = create_diffuse_material(device, 0.0, 1.0f, 0.0f, 1.0f);
1494 viewport = create_viewport(device, 0, 0, 640, 480);
1495 viewport_set_background(device, viewport, background);
1497 memset(&surface_desc, 0, sizeof(surface_desc));
1498 surface_desc.dwSize = sizeof(surface_desc);
1499 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1500 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1501 surface_desc.dwWidth = 256;
1502 surface_desc.dwHeight = 256;
1503 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
1504 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
1505 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
1506 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1507 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1508 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
1509 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1510 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1511 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1512 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1513 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
1514 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1515 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
1516 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
1517 IDirect3DTexture_Release(texture);
1519 memset(&fx, 0, sizeof(fx));
1520 fx.dwSize = sizeof(fx);
1521 U5(fx).dwFillColor = 0x000000ff;
1522 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1523 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1525 memset(&exec_desc, 0, sizeof(exec_desc));
1526 exec_desc.dwSize = sizeof(exec_desc);
1527 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
1528 exec_desc.dwBufferSize = 1024;
1529 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
1530 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
1531 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
1533 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
1534 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
1535 memcpy(exec_desc.lpData, tquad, sizeof(tquad));
1536 ptr = (BYTE *)exec_desc.lpData + sizeof(tquad);
1537 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1538 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
1539 emit_tquad(&ptr, 0);
1540 emit_end(&ptr);
1541 draw1_offset = sizeof(tquad);
1542 draw1_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw1_offset;
1543 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1544 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, FALSE);
1545 emit_tquad(&ptr, 0);
1546 emit_end(&ptr);
1547 draw2_offset = draw1_offset + draw1_len;
1548 draw2_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw2_offset;
1549 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1550 emit_tquad(&ptr, 0);
1551 emit_end(&ptr);
1552 draw3_offset = draw2_offset + draw2_len;
1553 draw3_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw3_offset;
1554 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 4);
1555 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1556 emit_tquad(&ptr, 0);
1557 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
1558 emit_end(&ptr);
1559 draw4_offset = draw3_offset + draw3_len;
1560 draw4_len = (BYTE *)ptr - (BYTE *)exec_desc.lpData - draw4_offset;
1561 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
1562 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
1564 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1565 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1566 hr = IDirect3DDevice_BeginScene(device);
1567 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1568 set_execute_data(execute_buffer, 4, draw1_offset, draw1_len);
1569 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1570 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1571 hr = IDirect3DDevice_EndScene(device);
1572 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1573 color = get_surface_color(rt, 320, 240);
1574 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1576 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1577 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1578 hr = IDirect3DDevice_BeginScene(device);
1579 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1580 set_execute_data(execute_buffer, 4, draw2_offset, draw2_len);
1581 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1582 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1583 hr = IDirect3DDevice_EndScene(device);
1584 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1585 color = get_surface_color(rt, 320, 240);
1586 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1588 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1589 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1590 hr = IDirect3DDevice_BeginScene(device);
1591 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1592 set_execute_data(execute_buffer, 4, draw3_offset, draw3_len);
1593 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1594 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1595 hr = IDirect3DDevice_EndScene(device);
1596 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1597 color = get_surface_color(rt, 320, 240);
1598 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1600 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
1601 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1602 hr = IDirect3DDevice_BeginScene(device);
1603 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1604 set_execute_data(execute_buffer, 4, draw4_offset, draw4_len);
1605 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
1606 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
1607 hr = IDirect3DDevice_EndScene(device);
1608 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1609 color = get_surface_color(rt, 320, 240);
1610 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1612 IDirect3DExecuteBuffer_Release(execute_buffer);
1613 IDirectDrawSurface_Release(surface);
1614 destroy_viewport(device, viewport);
1615 destroy_material(background);
1616 IDirectDrawSurface_Release(rt);
1617 IDirect3DDevice_Release(device);
1618 IDirectDraw_Release(ddraw);
1619 DestroyWindow(window);
1622 static void test_ck_complex(void)
1624 IDirectDrawSurface *surface, *mipmap, *tmp;
1625 DDSCAPS caps = {DDSCAPS_COMPLEX};
1626 DDSURFACEDESC surface_desc;
1627 IDirect3DDevice *device;
1628 DDCOLORKEY color_key;
1629 IDirectDraw *ddraw;
1630 unsigned int i;
1631 ULONG refcount;
1632 HWND window;
1633 HRESULT hr;
1635 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1636 0, 0, 640, 480, 0, 0, 0, 0);
1637 ddraw = create_ddraw();
1638 ok(!!ddraw, "Failed to create a ddraw object.\n");
1639 if (!(device = create_device(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1641 skip("Failed to create a 3D device, skipping test.\n");
1642 DestroyWindow(window);
1643 IDirectDraw2_Release(ddraw);
1644 return;
1646 IDirect3DDevice_Release(device);
1648 memset(&surface_desc, 0, sizeof(surface_desc));
1649 surface_desc.dwSize = sizeof(surface_desc);
1650 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1651 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1652 surface_desc.dwWidth = 128;
1653 surface_desc.dwHeight = 128;
1654 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1655 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1657 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1658 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1659 color_key.dwColorSpaceLowValue = 0x0000ff00;
1660 color_key.dwColorSpaceHighValue = 0x0000ff00;
1661 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1662 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1663 memset(&color_key, 0, sizeof(color_key));
1664 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1665 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1666 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1667 color_key.dwColorSpaceLowValue);
1668 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1669 color_key.dwColorSpaceHighValue);
1671 mipmap = surface;
1672 IDirectDrawSurface_AddRef(mipmap);
1673 for (i = 0; i < 7; ++i)
1675 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
1676 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
1678 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1679 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1680 color_key.dwColorSpaceLowValue = 0x000000ff;
1681 color_key.dwColorSpaceHighValue = 0x000000ff;
1682 hr = IDirectDrawSurface_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1683 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x, i %u.\n", hr, i);
1684 memset(&color_key, 0, sizeof(color_key));
1685 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1686 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x, i %u.\n", hr, i);
1687 ok(color_key.dwColorSpaceLowValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1688 color_key.dwColorSpaceLowValue, i);
1689 ok(color_key.dwColorSpaceHighValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1690 color_key.dwColorSpaceHighValue, i);
1692 IDirectDrawSurface_Release(mipmap);
1693 mipmap = tmp;
1696 memset(&color_key, 0, sizeof(color_key));
1697 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1698 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1699 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1700 color_key.dwColorSpaceLowValue);
1701 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1702 color_key.dwColorSpaceHighValue);
1704 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
1705 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
1706 IDirectDrawSurface_Release(mipmap);
1707 refcount = IDirectDrawSurface_Release(surface);
1708 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1710 memset(&surface_desc, 0, sizeof(surface_desc));
1711 surface_desc.dwSize = sizeof(surface_desc);
1712 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
1713 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
1714 surface_desc.dwBackBufferCount = 1;
1715 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1716 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1718 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1719 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1720 color_key.dwColorSpaceLowValue = 0x0000ff00;
1721 color_key.dwColorSpaceHighValue = 0x0000ff00;
1722 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1723 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1724 memset(&color_key, 0, sizeof(color_key));
1725 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1726 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1727 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1728 color_key.dwColorSpaceLowValue);
1729 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1730 color_key.dwColorSpaceHighValue);
1732 hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &tmp);
1733 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
1735 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1736 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1737 color_key.dwColorSpaceLowValue = 0x0000ff00;
1738 color_key.dwColorSpaceHighValue = 0x0000ff00;
1739 hr = IDirectDrawSurface_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1740 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1741 memset(&color_key, 0, sizeof(color_key));
1742 hr = IDirectDrawSurface_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1743 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1744 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1745 color_key.dwColorSpaceLowValue);
1746 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1747 color_key.dwColorSpaceHighValue);
1749 IDirectDrawSurface_Release(tmp);
1751 refcount = IDirectDrawSurface_Release(surface);
1752 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1753 refcount = IDirectDraw_Release(ddraw);
1754 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
1755 DestroyWindow(window);
1758 struct qi_test
1760 REFIID iid;
1761 REFIID refcount_iid;
1762 HRESULT hr;
1765 static void test_qi(const char *test_name, IUnknown *base_iface,
1766 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
1768 ULONG refcount, expected_refcount;
1769 IUnknown *iface1, *iface2;
1770 HRESULT hr;
1771 UINT i, j;
1773 for (i = 0; i < entry_count; ++i)
1775 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
1776 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
1777 if (SUCCEEDED(hr))
1779 for (j = 0; j < entry_count; ++j)
1781 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
1782 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
1783 if (SUCCEEDED(hr))
1785 expected_refcount = 0;
1786 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
1787 ++expected_refcount;
1788 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
1789 ++expected_refcount;
1790 refcount = IUnknown_Release(iface2);
1791 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
1792 refcount, test_name, i, j, expected_refcount);
1796 expected_refcount = 0;
1797 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
1798 ++expected_refcount;
1799 refcount = IUnknown_Release(iface1);
1800 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
1801 refcount, test_name, i, expected_refcount);
1806 static void test_surface_qi(void)
1808 static const struct qi_test tests[] =
1810 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface, S_OK },
1811 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface, S_OK },
1812 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
1813 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
1814 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
1815 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
1816 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
1817 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
1818 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
1819 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
1820 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
1821 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
1822 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
1823 {&IID_IDirect3D7, NULL, E_INVALIDARG },
1824 {&IID_IDirect3D3, NULL, E_INVALIDARG },
1825 {&IID_IDirect3D2, NULL, E_INVALIDARG },
1826 {&IID_IDirect3D, NULL, E_INVALIDARG },
1827 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
1828 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
1829 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
1830 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
1831 {&IID_IDirectDraw, NULL, E_INVALIDARG },
1832 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
1833 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
1834 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
1835 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
1836 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
1837 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
1838 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
1839 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
1840 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
1841 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
1842 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
1843 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
1844 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
1847 IDirectDrawSurface *surface;
1848 DDSURFACEDESC surface_desc;
1849 IDirect3DDevice *device;
1850 IDirectDraw *ddraw;
1851 HWND window;
1852 HRESULT hr;
1854 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1856 win_skip("DirectDrawCreateEx not available, skipping test.\n");
1857 return;
1860 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1861 0, 0, 640, 480, 0, 0, 0, 0);
1862 ddraw = create_ddraw();
1863 ok(!!ddraw, "Failed to create a ddraw object.\n");
1864 /* Try to create a D3D device to see if the ddraw implementation supports
1865 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
1866 * doesn't support e.g. the IDirect3DTexture interfaces. */
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;
1874 IDirect3DDevice_Release(device);
1876 memset(&surface_desc, 0, sizeof(surface_desc));
1877 surface_desc.dwSize = sizeof(surface_desc);
1878 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1879 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1880 surface_desc.dwWidth = 512;
1881 surface_desc.dwHeight = 512;
1882 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1883 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1885 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1887 IDirectDrawSurface_Release(surface);
1888 IDirectDraw_Release(ddraw);
1889 DestroyWindow(window);
1892 static void test_device_qi(void)
1894 static const struct qi_test tests[] =
1896 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface, S_OK },
1897 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface, S_OK },
1898 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
1899 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
1900 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
1901 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
1902 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
1903 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
1904 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
1905 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
1906 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
1907 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
1908 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
1909 {&IID_IDirect3DHALDevice, &IID_IDirectDrawSurface, S_OK },
1910 {&IID_IDirect3D7, NULL, E_INVALIDARG },
1911 {&IID_IDirect3D3, NULL, E_INVALIDARG },
1912 {&IID_IDirect3D2, NULL, E_INVALIDARG },
1913 {&IID_IDirect3D, NULL, E_INVALIDARG },
1914 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
1915 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
1916 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
1917 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
1918 {&IID_IDirectDraw, NULL, E_INVALIDARG },
1919 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
1920 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
1921 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
1922 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
1923 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
1924 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
1925 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
1926 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
1927 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
1928 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
1929 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
1930 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
1931 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
1935 IDirect3DDevice *device;
1936 IDirectDraw *ddraw;
1937 HWND window;
1939 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
1941 win_skip("DirectDrawCreateEx not available, skipping test.\n");
1942 return;
1945 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1946 0, 0, 640, 480, 0, 0, 0, 0);
1947 ddraw = create_ddraw();
1948 ok(!!ddraw, "Failed to create a ddraw object.\n");
1949 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
1951 skip("Failed to create a 3D device, skipping test.\n");
1952 IDirectDraw_Release(ddraw);
1953 DestroyWindow(window);
1954 return;
1957 test_qi("device_qi", (IUnknown *)device, &IID_IDirectDrawSurface, tests, sizeof(tests) / sizeof(*tests));
1959 IDirect3DDevice_Release(device);
1960 IDirectDraw_Release(ddraw);
1961 DestroyWindow(window);
1964 static void test_wndproc(void)
1966 LONG_PTR proc, ddraw_proc;
1967 IDirectDraw *ddraw;
1968 WNDCLASSA wc = {0};
1969 HWND window;
1970 HRESULT hr;
1971 ULONG ref;
1973 static struct message messages[] =
1975 {WM_WINDOWPOSCHANGING, FALSE, 0},
1976 {WM_MOVE, FALSE, 0},
1977 {WM_SIZE, FALSE, 0},
1978 {WM_WINDOWPOSCHANGING, FALSE, 0},
1979 {WM_ACTIVATE, FALSE, 0},
1980 {WM_SETFOCUS, FALSE, 0},
1981 {0, FALSE, 0},
1984 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
1985 ddraw = create_ddraw();
1986 ok(!!ddraw, "Failed to create a ddraw object.\n");
1988 wc.lpfnWndProc = test_proc;
1989 wc.lpszClassName = "ddraw_test_wndproc_wc";
1990 ok(RegisterClassA(&wc), "Failed to register window class.\n");
1992 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
1993 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
1995 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
1996 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
1997 (LONG_PTR)test_proc, proc);
1998 expect_messages = messages;
1999 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2000 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2001 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2002 expect_messages = NULL;
2003 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2004 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2005 (LONG_PTR)test_proc, proc);
2006 ref = IDirectDraw_Release(ddraw);
2007 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2008 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2009 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2010 (LONG_PTR)test_proc, proc);
2012 /* DDSCL_NORMAL doesn't. */
2013 ddraw = create_ddraw();
2014 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2015 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2016 (LONG_PTR)test_proc, proc);
2017 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2018 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2019 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2020 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2021 (LONG_PTR)test_proc, proc);
2022 ref = IDirectDraw_Release(ddraw);
2023 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2024 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2025 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2026 (LONG_PTR)test_proc, proc);
2028 /* The original window proc is only restored by ddraw if the current
2029 * window proc matches the one ddraw set. This also affects switching
2030 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
2031 ddraw = create_ddraw();
2032 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2033 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2034 (LONG_PTR)test_proc, proc);
2035 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2036 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2037 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2038 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2039 (LONG_PTR)test_proc, proc);
2040 ddraw_proc = proc;
2041 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2042 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2043 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2044 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2045 (LONG_PTR)test_proc, proc);
2046 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2047 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2048 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2049 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2050 (LONG_PTR)test_proc, proc);
2051 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2052 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2053 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2054 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2055 (LONG_PTR)DefWindowProcA, proc);
2056 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2057 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2058 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
2059 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2060 (LONG_PTR)DefWindowProcA, proc);
2061 ref = IDirectDraw_Release(ddraw);
2062 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2063 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2064 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2065 (LONG_PTR)test_proc, proc);
2067 ddraw = create_ddraw();
2068 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2069 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2070 (LONG_PTR)test_proc, proc);
2071 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2072 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2073 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2074 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2075 (LONG_PTR)test_proc, proc);
2076 ref = IDirectDraw_Release(ddraw);
2077 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2078 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2079 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2080 (LONG_PTR)DefWindowProcA, proc);
2082 fix_wndproc(window, (LONG_PTR)test_proc);
2083 expect_messages = NULL;
2084 DestroyWindow(window);
2085 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2088 static void test_window_style(void)
2090 LONG style, exstyle, tmp, expected_style;
2091 RECT fullscreen_rect, r;
2092 IDirectDraw *ddraw;
2093 HWND window;
2094 HRESULT hr;
2095 ULONG ref;
2096 BOOL ret;
2098 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2099 0, 0, 100, 100, 0, 0, 0, 0);
2100 ddraw = create_ddraw();
2101 ok(!!ddraw, "Failed to create a ddraw object.\n");
2103 style = GetWindowLongA(window, GWL_STYLE);
2104 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2105 SetRect(&fullscreen_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2107 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2108 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2110 tmp = GetWindowLongA(window, GWL_STYLE);
2111 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2112 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2113 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2115 GetWindowRect(window, &r);
2116 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2117 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2118 r.left, r.top, r.right, r.bottom);
2119 GetClientRect(window, &r);
2120 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2122 ret = SetForegroundWindow(GetDesktopWindow());
2123 ok(ret, "Failed to set foreground window.\n");
2125 tmp = GetWindowLongA(window, GWL_STYLE);
2126 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2127 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2128 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2130 ret = SetForegroundWindow(window);
2131 ok(ret, "Failed to set foreground window.\n");
2132 /* Windows 7 (but not Vista and XP) shows the window when it receives focus. Hide it again,
2133 * the next tests expect this. */
2134 ShowWindow(window, SW_HIDE);
2136 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2137 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2139 tmp = GetWindowLongA(window, GWL_STYLE);
2140 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2141 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2142 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2144 ShowWindow(window, SW_SHOW);
2145 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2146 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2148 tmp = GetWindowLongA(window, GWL_STYLE);
2149 expected_style = style | WS_VISIBLE;
2150 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2151 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2152 expected_style = exstyle | WS_EX_TOPMOST;
2153 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2155 ret = SetForegroundWindow(GetDesktopWindow());
2156 ok(ret, "Failed to set foreground window.\n");
2157 tmp = GetWindowLongA(window, GWL_STYLE);
2158 expected_style = style | WS_VISIBLE | WS_MINIMIZE;
2159 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2160 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2161 expected_style = exstyle | WS_EX_TOPMOST;
2162 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2164 ref = IDirectDraw_Release(ddraw);
2165 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2167 DestroyWindow(window);
2170 static void test_redundant_mode_set(void)
2172 DDSURFACEDESC surface_desc = {0};
2173 IDirectDraw *ddraw;
2174 HWND window;
2175 HRESULT hr;
2176 RECT r, s;
2177 ULONG ref;
2179 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2180 0, 0, 100, 100, 0, 0, 0, 0);
2181 ddraw = create_ddraw();
2182 ok(!!ddraw, "Failed to create a ddraw object.\n");
2184 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2185 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2187 surface_desc.dwSize = sizeof(surface_desc);
2188 hr = IDirectDraw_GetDisplayMode(ddraw, &surface_desc);
2189 ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
2191 hr = IDirectDraw_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2192 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
2193 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2195 GetWindowRect(window, &r);
2196 r.right /= 2;
2197 r.bottom /= 2;
2198 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2199 GetWindowRect(window, &s);
2200 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2201 r.left, r.top, r.right, r.bottom,
2202 s.left, s.top, s.right, s.bottom);
2204 hr = IDirectDraw_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2205 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount);
2206 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2208 GetWindowRect(window, &s);
2209 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2210 r.left, r.top, r.right, r.bottom,
2211 s.left, s.top, s.right, s.bottom);
2213 ref = IDirectDraw_Release(ddraw);
2214 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2216 DestroyWindow(window);
2219 static SIZE screen_size;
2221 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2223 if (message == WM_SIZE)
2225 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2226 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2229 return test_proc(hwnd, message, wparam, lparam);
2232 struct test_coop_level_mode_set_enum_param
2234 DWORD ddraw_width, ddraw_height, user32_width, user32_height;
2237 static HRESULT CALLBACK test_coop_level_mode_set_enum_cb(DDSURFACEDESC *surface_desc, void *context)
2239 struct test_coop_level_mode_set_enum_param *param = context;
2241 if (U1(surface_desc->ddpfPixelFormat).dwRGBBitCount != registry_mode.dmBitsPerPel)
2242 return DDENUMRET_OK;
2243 if (surface_desc->dwWidth == registry_mode.dmPelsWidth
2244 && surface_desc->dwHeight == registry_mode.dmPelsHeight)
2245 return DDENUMRET_OK;
2247 if (!param->ddraw_width)
2249 param->ddraw_width = surface_desc->dwWidth;
2250 param->ddraw_height = surface_desc->dwHeight;
2251 return DDENUMRET_OK;
2253 if (surface_desc->dwWidth == param->ddraw_width && surface_desc->dwHeight == param->ddraw_height)
2254 return DDENUMRET_OK;
2256 param->user32_width = surface_desc->dwWidth;
2257 param->user32_height = surface_desc->dwHeight;
2258 return DDENUMRET_CANCEL;
2261 static void test_coop_level_mode_set(void)
2263 IDirectDrawSurface *primary;
2264 RECT registry_rect, ddraw_rect, user32_rect, r;
2265 IDirectDraw *ddraw;
2266 DDSURFACEDESC ddsd;
2267 WNDCLASSA wc = {0};
2268 HWND window;
2269 HRESULT hr;
2270 ULONG ref;
2271 MSG msg;
2272 struct test_coop_level_mode_set_enum_param param;
2273 DEVMODEW devmode;
2274 BOOL ret;
2275 LONG change_ret;
2277 static const struct message exclusive_messages[] =
2279 {WM_WINDOWPOSCHANGING, FALSE, 0},
2280 {WM_WINDOWPOSCHANGED, FALSE, 0},
2281 {WM_SIZE, FALSE, 0},
2282 {WM_DISPLAYCHANGE, FALSE, 0},
2283 {0, FALSE, 0},
2285 static const struct message exclusive_focus_loss_messages[] =
2287 {WM_ACTIVATE, TRUE, WA_INACTIVE},
2288 {WM_DISPLAYCHANGE, FALSE, 0},
2289 {WM_WINDOWPOSCHANGING, FALSE, 0},
2290 /* Like d3d8 and d3d9 ddraw seems to use SW_SHOWMINIMIZED instead of
2291 * SW_MINIMIZED, causing a recursive window activation that does not
2292 * produce the same result in Wine yet. Ignore the difference for now.
2293 * {WM_ACTIVATE, TRUE, 0x200000 | WA_ACTIVE}, */
2294 {WM_WINDOWPOSCHANGED, FALSE, 0},
2295 {WM_MOVE, FALSE, 0},
2296 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2297 {WM_ACTIVATEAPP, TRUE, FALSE},
2298 {0, FALSE, 0},
2300 static const struct message exclusive_focus_restore_messages[] =
2302 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* From the ShowWindow(SW_RESTORE). */
2303 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* Generated by ddraw, matches d3d9 behavior. */
2304 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching previous message. */
2305 {WM_SIZE, FALSE, 0}, /* DefWindowProc. */
2306 {WM_DISPLAYCHANGE, FALSE, 0}, /* Ddraw restores mode. */
2307 /* Native redundantly sets the window size here. */
2308 {WM_ACTIVATEAPP, TRUE, TRUE}, /* End of ddraw's hooks. */
2309 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching the one from ShowWindow. */
2310 {WM_MOVE, FALSE, 0}, /* DefWindowProc. */
2311 {WM_SIZE, TRUE, SIZE_RESTORED}, /* DefWindowProc. */
2312 {0, FALSE, 0},
2314 static const struct message sc_restore_messages[] =
2316 {WM_SYSCOMMAND, TRUE, SC_RESTORE},
2317 {WM_WINDOWPOSCHANGING, FALSE, 0},
2318 {WM_WINDOWPOSCHANGED, FALSE, 0},
2319 {WM_SIZE, TRUE, SIZE_RESTORED},
2320 {0, FALSE, 0},
2322 static const struct message sc_minimize_messages[] =
2324 {WM_SYSCOMMAND, TRUE, SC_MINIMIZE},
2325 {WM_WINDOWPOSCHANGING, FALSE, 0},
2326 {WM_WINDOWPOSCHANGED, FALSE, 0},
2327 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2328 {0, FALSE, 0},
2330 static const struct message sc_maximize_messages[] =
2332 {WM_SYSCOMMAND, TRUE, SC_MAXIMIZE},
2333 {WM_WINDOWPOSCHANGING, FALSE, 0},
2334 {WM_WINDOWPOSCHANGED, FALSE, 0},
2335 {WM_SIZE, TRUE, SIZE_MAXIMIZED},
2336 {0, FALSE, 0},
2339 static const struct message normal_messages[] =
2341 {WM_DISPLAYCHANGE, FALSE, 0},
2342 {0, FALSE, 0},
2345 ddraw = create_ddraw();
2346 ok(!!ddraw, "Failed to create a ddraw object.\n");
2348 memset(&param, 0, sizeof(param));
2349 hr = IDirectDraw_EnumDisplayModes(ddraw, 0, NULL, &param, test_coop_level_mode_set_enum_cb);
2350 ok(SUCCEEDED(hr), "Failed to enumerate display mode, hr %#x.\n", hr);
2351 ref = IDirectDraw_Release(ddraw);
2352 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2354 if (!param.user32_height)
2356 skip("Fewer than 3 different modes supported, skipping mode restore test.\n");
2357 return;
2360 SetRect(&registry_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2361 SetRect(&ddraw_rect, 0, 0, param.ddraw_width, param.ddraw_height);
2362 SetRect(&user32_rect, 0, 0, param.user32_width, param.user32_height);
2364 memset(&devmode, 0, sizeof(devmode));
2365 devmode.dmSize = sizeof(devmode);
2366 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2367 devmode.dmPelsWidth = param.user32_width;
2368 devmode.dmPelsHeight = param.user32_height;
2369 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2370 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2372 ddraw = create_ddraw();
2373 ok(!!ddraw, "Failed to create a ddraw object.\n");
2375 wc.lpfnWndProc = mode_set_proc;
2376 wc.lpszClassName = "ddraw_test_wndproc_wc";
2377 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2379 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2380 0, 0, 100, 100, 0, 0, 0, 0);
2382 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2383 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2385 GetWindowRect(window, &r);
2386 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2387 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2388 r.left, r.top, r.right, r.bottom);
2390 memset(&ddsd, 0, sizeof(ddsd));
2391 ddsd.dwSize = sizeof(ddsd);
2392 ddsd.dwFlags = DDSD_CAPS;
2393 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2395 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2396 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2397 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2398 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2399 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2400 param.user32_width, ddsd.dwWidth);
2401 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2402 param.user32_height, ddsd.dwHeight);
2404 GetWindowRect(window, &r);
2405 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2406 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2407 r.left, r.top, r.right, r.bottom);
2409 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2410 expect_messages = exclusive_messages;
2411 screen_size.cx = 0;
2412 screen_size.cy = 0;
2414 hr = IDirectDrawSurface_IsLost(primary);
2415 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2416 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2417 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2418 hr = IDirectDrawSurface_IsLost(primary);
2419 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2421 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2422 expect_messages = NULL;
2423 ok(screen_size.cx == param.ddraw_width && screen_size.cy == param.ddraw_height,
2424 "Expected screen size %ux%u, got %ux%u.\n",
2425 param.ddraw_width, param.ddraw_height, screen_size.cx, screen_size.cy);
2427 GetWindowRect(window, &r);
2428 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2429 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2430 r.left, r.top, r.right, r.bottom);
2432 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2433 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2434 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2435 param.user32_width, ddsd.dwWidth);
2436 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2437 param.user32_height, ddsd.dwHeight);
2438 IDirectDrawSurface_Release(primary);
2440 memset(&ddsd, 0, sizeof(ddsd));
2441 ddsd.dwSize = sizeof(ddsd);
2442 ddsd.dwFlags = DDSD_CAPS;
2443 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2445 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2446 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2447 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2448 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2449 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2450 param.ddraw_width, ddsd.dwWidth);
2451 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2452 param.ddraw_height, ddsd.dwHeight);
2454 GetWindowRect(window, &r);
2455 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2456 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2457 r.left, r.top, r.right, r.bottom);
2459 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2460 expect_messages = exclusive_messages;
2461 screen_size.cx = 0;
2462 screen_size.cy = 0;
2464 hr = IDirectDrawSurface_IsLost(primary);
2465 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2466 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2467 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2468 hr = IDirectDrawSurface_IsLost(primary);
2469 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2471 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2472 expect_messages = NULL;
2473 ok(screen_size.cx == param.user32_width && screen_size.cy == param.user32_height,
2474 "Expected screen size %ux%u, got %ux%u.\n",
2475 param.user32_width, param.user32_height, screen_size.cx, screen_size.cy);
2477 GetWindowRect(window, &r);
2478 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2479 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2480 r.left, r.top, r.right, r.bottom);
2482 expect_messages = exclusive_focus_loss_messages;
2483 ret = SetForegroundWindow(GetDesktopWindow());
2484 ok(ret, "Failed to set foreground window.\n");
2485 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2486 memset(&devmode, 0, sizeof(devmode));
2487 devmode.dmSize = sizeof(devmode);
2488 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2489 ok(ret, "Failed to get display mode.\n");
2490 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2491 && devmode.dmPelsHeight == registry_mode.dmPelsHeight, "Got unexpect screen size %ux%u.\n",
2492 devmode.dmPelsWidth, devmode.dmPelsHeight);
2494 expect_messages = exclusive_focus_restore_messages;
2495 ShowWindow(window, SW_RESTORE);
2496 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2498 GetWindowRect(window, &r);
2499 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2500 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2501 r.left, r.top, r.right, r.bottom);
2502 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2503 ok(ret, "Failed to get display mode.\n");
2504 ok(devmode.dmPelsWidth == param.ddraw_width
2505 && devmode.dmPelsHeight == param.ddraw_height, "Got unexpect screen size %ux%u.\n",
2506 devmode.dmPelsWidth, devmode.dmPelsHeight);
2508 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2509 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2510 /* Normally the primary should be restored here. Unfortunately this causes the
2511 * GetSurfaceDesc call after the next display mode change to crash on the Windows 8
2512 * testbot. Another Restore call would presumably avoid the crash, but it also moots
2513 * the point of the GetSurfaceDesc call. */
2515 expect_messages = sc_minimize_messages;
2516 SendMessageA(window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
2517 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2518 expect_messages = NULL;
2520 expect_messages = sc_restore_messages;
2521 SendMessageA(window, WM_SYSCOMMAND, SC_RESTORE, 0);
2522 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2523 expect_messages = NULL;
2525 expect_messages = sc_maximize_messages;
2526 SendMessageA(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
2527 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2528 expect_messages = NULL;
2530 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2531 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2533 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2534 expect_messages = exclusive_messages;
2535 screen_size.cx = 0;
2536 screen_size.cy = 0;
2538 hr = IDirectDrawSurface_IsLost(primary);
2539 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2540 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2541 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2542 hr = IDirectDrawSurface_IsLost(primary);
2543 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2545 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2546 expect_messages = NULL;
2547 ok(screen_size.cx == registry_mode.dmPelsWidth
2548 && screen_size.cy == registry_mode.dmPelsHeight,
2549 "Expected screen size %ux%u, got %ux%u.\n",
2550 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size.cx, screen_size.cy);
2552 GetWindowRect(window, &r);
2553 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2554 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2555 r.left, r.top, r.right, r.bottom);
2557 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2558 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2559 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2560 param.ddraw_width, ddsd.dwWidth);
2561 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2562 param.ddraw_height, ddsd.dwHeight);
2563 IDirectDrawSurface_Release(primary);
2565 /* For Wine. */
2566 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2567 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2569 memset(&ddsd, 0, sizeof(ddsd));
2570 ddsd.dwSize = sizeof(ddsd);
2571 ddsd.dwFlags = DDSD_CAPS;
2572 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2574 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2575 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2576 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2577 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2578 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2579 registry_mode.dmPelsWidth, ddsd.dwWidth);
2580 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2581 registry_mode.dmPelsHeight, ddsd.dwHeight);
2583 GetWindowRect(window, &r);
2584 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2585 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2586 r.left, r.top, r.right, r.bottom);
2588 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2589 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2591 GetWindowRect(window, &r);
2592 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2593 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2594 r.left, r.top, r.right, r.bottom);
2596 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2597 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2598 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2599 registry_mode.dmPelsWidth, ddsd.dwWidth);
2600 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2601 registry_mode.dmPelsHeight, ddsd.dwHeight);
2602 IDirectDrawSurface_Release(primary);
2604 memset(&ddsd, 0, sizeof(ddsd));
2605 ddsd.dwSize = sizeof(ddsd);
2606 ddsd.dwFlags = DDSD_CAPS;
2607 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2609 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2610 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2611 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2612 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2613 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2614 registry_mode.dmPelsWidth, ddsd.dwWidth);
2615 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2616 registry_mode.dmPelsHeight, ddsd.dwHeight);
2618 GetWindowRect(window, &r);
2619 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2620 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2621 r.left, r.top, r.right, r.bottom);
2623 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2624 expect_messages = normal_messages;
2625 screen_size.cx = 0;
2626 screen_size.cy = 0;
2628 hr = IDirectDrawSurface_IsLost(primary);
2629 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2630 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2631 devmode.dmPelsWidth = param.user32_width;
2632 devmode.dmPelsHeight = param.user32_height;
2633 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2634 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2635 hr = IDirectDrawSurface_IsLost(primary);
2636 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2638 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2639 expect_messages = NULL;
2640 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2642 GetWindowRect(window, &r);
2643 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2644 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2645 r.left, r.top, r.right, r.bottom);
2647 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2648 expect_messages = normal_messages;
2649 screen_size.cx = 0;
2650 screen_size.cy = 0;
2652 hr = IDirectDrawSurface_Restore(primary);
2653 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2654 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2655 if (hr == DDERR_NOEXCLUSIVEMODE /* NT4 testbot */)
2657 win_skip("Broken SetDisplayMode(), skipping remaining tests.\n");
2658 IDirectDrawSurface_Release(primary);
2659 IDirectDraw_Release(ddraw);
2660 goto done;
2662 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2663 hr = IDirectDrawSurface_Restore(primary);
2664 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2665 hr = IDirectDrawSurface_IsLost(primary);
2666 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2668 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2669 expect_messages = NULL;
2670 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2672 GetWindowRect(window, &r);
2673 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2674 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2675 r.left, r.top, r.right, r.bottom);
2677 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2678 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2679 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2680 registry_mode.dmPelsWidth, ddsd.dwWidth);
2681 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2682 registry_mode.dmPelsHeight, ddsd.dwHeight);
2683 IDirectDrawSurface_Release(primary);
2685 memset(&ddsd, 0, sizeof(ddsd));
2686 ddsd.dwSize = sizeof(ddsd);
2687 ddsd.dwFlags = DDSD_CAPS;
2688 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2690 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2691 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2692 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2693 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2694 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2695 param.ddraw_width, ddsd.dwWidth);
2696 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2697 param.ddraw_height, ddsd.dwHeight);
2699 GetWindowRect(window, &r);
2700 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2701 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2702 r.left, r.top, r.right, r.bottom);
2704 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2705 expect_messages = normal_messages;
2706 screen_size.cx = 0;
2707 screen_size.cy = 0;
2709 hr = IDirectDrawSurface_IsLost(primary);
2710 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2711 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2712 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2713 hr = IDirectDrawSurface_IsLost(primary);
2714 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2716 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2717 expect_messages = NULL;
2718 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2720 GetWindowRect(window, &r);
2721 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2722 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2723 r.left, r.top, r.right, r.bottom);
2725 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2726 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2727 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2728 param.ddraw_width, ddsd.dwWidth);
2729 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2730 param.ddraw_height, ddsd.dwHeight);
2731 IDirectDrawSurface_Release(primary);
2733 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2734 ok(ret, "Failed to get display mode.\n");
2735 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2736 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
2737 "Expected resolution %ux%u, got %ux%u.\n",
2738 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
2739 devmode.dmPelsWidth, devmode.dmPelsHeight);
2740 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2741 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2743 memset(&ddsd, 0, sizeof(ddsd));
2744 ddsd.dwSize = sizeof(ddsd);
2745 ddsd.dwFlags = DDSD_CAPS;
2746 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2748 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2749 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2750 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2751 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2752 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2753 registry_mode.dmPelsWidth, ddsd.dwWidth);
2754 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2755 registry_mode.dmPelsHeight, ddsd.dwHeight);
2757 GetWindowRect(window, &r);
2758 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2759 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2760 r.left, r.top, r.right, r.bottom);
2762 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
2763 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
2764 * not DDSCL_FULLSCREEN. */
2765 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2766 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2768 GetWindowRect(window, &r);
2769 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2770 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2771 r.left, r.top, r.right, r.bottom);
2773 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2774 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2775 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2776 registry_mode.dmPelsWidth, ddsd.dwWidth);
2777 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2778 registry_mode.dmPelsHeight, ddsd.dwHeight);
2779 IDirectDrawSurface_Release(primary);
2781 memset(&ddsd, 0, sizeof(ddsd));
2782 ddsd.dwSize = sizeof(ddsd);
2783 ddsd.dwFlags = DDSD_CAPS;
2784 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2786 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2787 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2788 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2789 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2790 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2791 registry_mode.dmPelsWidth, ddsd.dwWidth);
2792 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2793 registry_mode.dmPelsHeight, ddsd.dwHeight);
2795 GetWindowRect(window, &r);
2796 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2797 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2798 r.left, r.top, r.right, r.bottom);
2800 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2801 expect_messages = normal_messages;
2802 screen_size.cx = 0;
2803 screen_size.cy = 0;
2805 hr = IDirectDrawSurface_IsLost(primary);
2806 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2807 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2808 devmode.dmPelsWidth = param.user32_width;
2809 devmode.dmPelsHeight = param.user32_height;
2810 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2811 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2812 hr = IDirectDrawSurface_IsLost(primary);
2813 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2815 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2816 expect_messages = NULL;
2817 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2819 GetWindowRect(window, &r);
2820 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2821 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2822 r.left, r.top, r.right, r.bottom);
2824 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2825 expect_messages = normal_messages;
2826 screen_size.cx = 0;
2827 screen_size.cy = 0;
2829 hr = IDirectDrawSurface_Restore(primary);
2830 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2831 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2832 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2833 hr = IDirectDrawSurface_Restore(primary);
2834 todo_wine ok(hr == DDERR_WRONGMODE, "Got unexpected hr %#x.\n", hr);
2835 hr = IDirectDrawSurface_IsLost(primary);
2836 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2838 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2839 expect_messages = NULL;
2840 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2842 GetWindowRect(window, &r);
2843 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2844 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2845 r.left, r.top, r.right, r.bottom);
2847 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2848 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2849 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2850 registry_mode.dmPelsWidth, ddsd.dwWidth);
2851 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2852 registry_mode.dmPelsHeight, ddsd.dwHeight);
2853 IDirectDrawSurface_Release(primary);
2855 memset(&ddsd, 0, sizeof(ddsd));
2856 ddsd.dwSize = sizeof(ddsd);
2857 ddsd.dwFlags = DDSD_CAPS;
2858 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2860 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2861 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2862 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2863 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2864 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2865 param.ddraw_width, ddsd.dwWidth);
2866 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2867 param.ddraw_height, ddsd.dwHeight);
2869 GetWindowRect(window, &r);
2870 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2871 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2872 r.left, r.top, r.right, r.bottom);
2874 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2875 expect_messages = normal_messages;
2876 screen_size.cx = 0;
2877 screen_size.cy = 0;
2879 hr = IDirectDrawSurface_IsLost(primary);
2880 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
2881 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2882 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2883 hr = IDirectDrawSurface_IsLost(primary);
2884 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
2886 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2887 expect_messages = NULL;
2888 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2890 GetWindowRect(window, &r);
2891 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2892 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2893 r.left, r.top, r.right, r.bottom);
2895 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2896 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2897 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2898 param.ddraw_width, ddsd.dwWidth);
2899 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2900 param.ddraw_height, ddsd.dwHeight);
2901 IDirectDrawSurface_Release(primary);
2903 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2904 ok(ret, "Failed to get display mode.\n");
2905 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2906 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
2907 "Expected resolution %ux%u, got %ux%u.\n",
2908 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
2909 devmode.dmPelsWidth, devmode.dmPelsHeight);
2910 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2911 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2913 memset(&ddsd, 0, sizeof(ddsd));
2914 ddsd.dwSize = sizeof(ddsd);
2915 ddsd.dwFlags = DDSD_CAPS;
2916 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2918 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2919 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2920 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2921 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2922 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2923 registry_mode.dmPelsWidth, ddsd.dwWidth);
2924 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2925 registry_mode.dmPelsHeight, ddsd.dwHeight);
2926 IDirectDrawSurface_Release(primary);
2928 GetWindowRect(window, &r);
2929 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2930 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2931 r.left, r.top, r.right, r.bottom);
2933 /* Unlike ddraw2-7, changing from EXCLUSIVE to NORMAL does not restore the resolution */
2934 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2935 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2936 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2937 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2939 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2940 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2942 memset(&ddsd, 0, sizeof(ddsd));
2943 ddsd.dwSize = sizeof(ddsd);
2944 ddsd.dwFlags = DDSD_CAPS;
2945 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2947 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
2948 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2949 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &ddsd);
2950 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2951 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2952 param.ddraw_width, ddsd.dwWidth);
2953 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2954 param.ddraw_height, ddsd.dwHeight);
2955 IDirectDrawSurface_Release(primary);
2956 hr = IDirectDraw_RestoreDisplayMode(ddraw);
2957 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2959 ref = IDirectDraw_Release(ddraw);
2960 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2962 GetWindowRect(window, &r);
2963 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2964 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2965 r.left, r.top, r.right, r.bottom);
2967 done:
2968 expect_messages = NULL;
2969 DestroyWindow(window);
2970 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2973 static void test_coop_level_mode_set_multi(void)
2975 IDirectDraw *ddraw1, *ddraw2;
2976 UINT w, h;
2977 HWND window;
2978 HRESULT hr;
2979 ULONG ref;
2981 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2982 0, 0, 100, 100, 0, 0, 0, 0);
2983 ddraw1 = create_ddraw();
2984 ok(!!ddraw1, "Failed to create a ddraw object.\n");
2986 /* With just a single ddraw object, the display mode is restored on
2987 * release. */
2988 hr = set_display_mode(ddraw1, 800, 600);
2989 if (hr == DDERR_NOEXCLUSIVEMODE /* NT4 testbot */)
2991 win_skip("Broken SetDisplayMode(), skipping test.\n");
2992 IDirectDraw_Release(ddraw1);
2993 DestroyWindow(window);
2994 return;
2996 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2997 w = GetSystemMetrics(SM_CXSCREEN);
2998 ok(w == 800, "Got unexpected screen width %u.\n", w);
2999 h = GetSystemMetrics(SM_CYSCREEN);
3000 ok(h == 600, "Got unexpected screen height %u.\n", h);
3002 ref = IDirectDraw_Release(ddraw1);
3003 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3004 w = GetSystemMetrics(SM_CXSCREEN);
3005 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3006 h = GetSystemMetrics(SM_CYSCREEN);
3007 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3009 /* When there are multiple ddraw objects, the display mode is restored to
3010 * the initial mode, before the first SetDisplayMode() call. */
3011 ddraw1 = create_ddraw();
3012 hr = set_display_mode(ddraw1, 800, 600);
3013 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3014 w = GetSystemMetrics(SM_CXSCREEN);
3015 ok(w == 800, "Got unexpected screen width %u.\n", w);
3016 h = GetSystemMetrics(SM_CYSCREEN);
3017 ok(h == 600, "Got unexpected screen height %u.\n", h);
3019 ddraw2 = create_ddraw();
3020 hr = set_display_mode(ddraw2, 640, 480);
3021 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3022 w = GetSystemMetrics(SM_CXSCREEN);
3023 ok(w == 640, "Got unexpected screen width %u.\n", w);
3024 h = GetSystemMetrics(SM_CYSCREEN);
3025 ok(h == 480, "Got unexpected screen height %u.\n", h);
3027 ref = IDirectDraw_Release(ddraw2);
3028 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3029 w = GetSystemMetrics(SM_CXSCREEN);
3030 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3031 h = GetSystemMetrics(SM_CYSCREEN);
3032 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3034 ref = IDirectDraw_Release(ddraw1);
3035 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3036 w = GetSystemMetrics(SM_CXSCREEN);
3037 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3038 h = GetSystemMetrics(SM_CYSCREEN);
3039 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3041 /* Regardless of release ordering. */
3042 ddraw1 = create_ddraw();
3043 hr = set_display_mode(ddraw1, 800, 600);
3044 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3045 w = GetSystemMetrics(SM_CXSCREEN);
3046 ok(w == 800, "Got unexpected screen width %u.\n", w);
3047 h = GetSystemMetrics(SM_CYSCREEN);
3048 ok(h == 600, "Got unexpected screen height %u.\n", h);
3050 ddraw2 = create_ddraw();
3051 hr = set_display_mode(ddraw2, 640, 480);
3052 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3053 w = GetSystemMetrics(SM_CXSCREEN);
3054 ok(w == 640, "Got unexpected screen width %u.\n", w);
3055 h = GetSystemMetrics(SM_CYSCREEN);
3056 ok(h == 480, "Got unexpected screen height %u.\n", h);
3058 ref = IDirectDraw_Release(ddraw1);
3059 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3060 w = GetSystemMetrics(SM_CXSCREEN);
3061 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3062 h = GetSystemMetrics(SM_CYSCREEN);
3063 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3065 ref = IDirectDraw_Release(ddraw2);
3066 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3067 w = GetSystemMetrics(SM_CXSCREEN);
3068 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3069 h = GetSystemMetrics(SM_CYSCREEN);
3070 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3072 /* But only for ddraw objects that called SetDisplayMode(). */
3073 ddraw1 = create_ddraw();
3074 ddraw2 = create_ddraw();
3075 hr = set_display_mode(ddraw2, 640, 480);
3076 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3077 w = GetSystemMetrics(SM_CXSCREEN);
3078 ok(w == 640, "Got unexpected screen width %u.\n", w);
3079 h = GetSystemMetrics(SM_CYSCREEN);
3080 ok(h == 480, "Got unexpected screen height %u.\n", h);
3082 ref = IDirectDraw_Release(ddraw1);
3083 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3084 w = GetSystemMetrics(SM_CXSCREEN);
3085 ok(w == 640, "Got unexpected screen width %u.\n", w);
3086 h = GetSystemMetrics(SM_CYSCREEN);
3087 ok(h == 480, "Got unexpected screen height %u.\n", h);
3089 ref = IDirectDraw_Release(ddraw2);
3090 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3091 w = GetSystemMetrics(SM_CXSCREEN);
3092 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3093 h = GetSystemMetrics(SM_CYSCREEN);
3094 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3096 /* If there's a ddraw object that's currently in exclusive mode, it blocks
3097 * restoring the display mode. */
3098 ddraw1 = create_ddraw();
3099 hr = set_display_mode(ddraw1, 800, 600);
3100 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3101 w = GetSystemMetrics(SM_CXSCREEN);
3102 ok(w == 800, "Got unexpected screen width %u.\n", w);
3103 h = GetSystemMetrics(SM_CYSCREEN);
3104 ok(h == 600, "Got unexpected screen height %u.\n", h);
3106 ddraw2 = create_ddraw();
3107 hr = set_display_mode(ddraw2, 640, 480);
3108 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3109 w = GetSystemMetrics(SM_CXSCREEN);
3110 ok(w == 640, "Got unexpected screen width %u.\n", w);
3111 h = GetSystemMetrics(SM_CYSCREEN);
3112 ok(h == 480, "Got unexpected screen height %u.\n", h);
3114 hr = IDirectDraw_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3115 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3117 ref = IDirectDraw_Release(ddraw1);
3118 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3119 w = GetSystemMetrics(SM_CXSCREEN);
3120 ok(w == 640, "Got unexpected screen width %u.\n", w);
3121 h = GetSystemMetrics(SM_CYSCREEN);
3122 ok(h == 480, "Got unexpected screen height %u.\n", h);
3124 ref = IDirectDraw_Release(ddraw2);
3125 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3126 w = GetSystemMetrics(SM_CXSCREEN);
3127 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3128 h = GetSystemMetrics(SM_CYSCREEN);
3129 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3131 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
3132 ddraw1 = create_ddraw();
3133 hr = set_display_mode(ddraw1, 800, 600);
3134 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3135 w = GetSystemMetrics(SM_CXSCREEN);
3136 ok(w == 800, "Got unexpected screen width %u.\n", w);
3137 h = GetSystemMetrics(SM_CYSCREEN);
3138 ok(h == 600, "Got unexpected screen height %u.\n", h);
3140 hr = IDirectDraw_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3141 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3143 ddraw2 = create_ddraw();
3144 hr = set_display_mode(ddraw2, 640, 480);
3145 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
3147 ref = IDirectDraw_Release(ddraw1);
3148 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3149 w = GetSystemMetrics(SM_CXSCREEN);
3150 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3151 h = GetSystemMetrics(SM_CYSCREEN);
3152 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3154 ref = IDirectDraw_Release(ddraw2);
3155 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3156 w = GetSystemMetrics(SM_CXSCREEN);
3157 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3158 h = GetSystemMetrics(SM_CYSCREEN);
3159 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3161 DestroyWindow(window);
3164 static void test_initialize(void)
3166 IDirectDraw *ddraw;
3167 IDirect3D *d3d;
3168 HRESULT hr;
3170 ddraw = create_ddraw();
3171 ok(!!ddraw, "Failed to create a ddraw object.\n");
3173 hr = IDirectDraw_Initialize(ddraw, NULL);
3174 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
3175 IDirectDraw_Release(ddraw);
3177 CoInitialize(NULL);
3178 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw, (void **)&ddraw);
3179 ok(SUCCEEDED(hr), "Failed to create IDirectDraw instance, hr %#x.\n", hr);
3180 hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D, (void **)&d3d);
3181 if (SUCCEEDED(hr))
3183 /* IDirect3D_Initialize() just returns DDERR_ALREADYINITIALIZED. */
3184 hr = IDirect3D_Initialize(d3d, NULL);
3185 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3186 IDirect3D_Release(d3d);
3188 else
3189 skip("D3D interface is not available, skipping test.\n");
3190 hr = IDirectDraw_Initialize(ddraw, NULL);
3191 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
3192 hr = IDirectDraw_Initialize(ddraw, NULL);
3193 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3194 IDirectDraw_Release(ddraw);
3195 CoUninitialize();
3197 if (0) /* This crashes on the W2KPROSP4 testbot. */
3199 CoInitialize(NULL);
3200 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirect3D, (void **)&d3d);
3201 ok(hr == E_NOINTERFACE, "CoCreateInstance returned hr %#x, expected E_NOINTERFACE.\n", hr);
3202 CoUninitialize();
3206 static void test_coop_level_surf_create(void)
3208 IDirectDrawSurface *surface;
3209 IDirectDraw *ddraw;
3210 DDSURFACEDESC ddsd;
3211 HRESULT hr;
3213 ddraw = create_ddraw();
3214 ok(!!ddraw, "Failed to create a ddraw object.\n");
3216 memset(&ddsd, 0, sizeof(ddsd));
3217 ddsd.dwSize = sizeof(ddsd);
3218 ddsd.dwFlags = DDSD_CAPS;
3219 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3220 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3221 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3223 IDirectDraw_Release(ddraw);
3226 static void test_coop_level_multi_window(void)
3228 HWND window1, window2;
3229 IDirectDraw *ddraw;
3230 HRESULT hr;
3232 window1 = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3233 0, 0, 640, 480, 0, 0, 0, 0);
3234 window2 = CreateWindowA("static", "ddraw_test2", WS_OVERLAPPEDWINDOW,
3235 0, 0, 640, 480, 0, 0, 0, 0);
3236 ddraw = create_ddraw();
3237 ok(!!ddraw, "Failed to create a ddraw object.\n");
3239 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3240 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3241 hr = IDirectDraw_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3242 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3243 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3244 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3246 IDirectDraw_Release(ddraw);
3247 DestroyWindow(window2);
3248 DestroyWindow(window1);
3251 static void test_clear_rect_count(void)
3253 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3254 IDirect3DMaterial *white, *red, *green, *blue;
3255 IDirect3DViewport *viewport;
3256 IDirect3DDevice *device;
3257 IDirectDrawSurface *rt;
3258 IDirectDraw *ddraw;
3259 D3DCOLOR color;
3260 HWND window;
3261 HRESULT hr;
3263 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3264 0, 0, 640, 480, 0, 0, 0, 0);
3265 ddraw = create_ddraw();
3266 ok(!!ddraw, "Failed to create a ddraw object.\n");
3267 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3269 skip("Failed to create a 3D device, skipping test.\n");
3270 IDirectDraw_Release(ddraw);
3271 DestroyWindow(window);
3272 return;
3275 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
3276 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3278 white = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
3279 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
3280 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
3281 blue = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
3282 viewport = create_viewport(device, 0, 0, 640, 480);
3284 viewport_set_background(device, viewport, white);
3285 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
3286 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3287 viewport_set_background(device, viewport, red);
3288 hr = IDirect3DViewport_Clear(viewport, 0, &clear_rect, D3DCLEAR_TARGET);
3289 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3290 viewport_set_background(device, viewport, green);
3291 hr = IDirect3DViewport_Clear(viewport, 0, NULL, D3DCLEAR_TARGET);
3292 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3293 viewport_set_background(device, viewport, blue);
3294 hr = IDirect3DViewport_Clear(viewport, 1, NULL, D3DCLEAR_TARGET);
3295 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
3297 color = get_surface_color(rt, 320, 240);
3298 ok(compare_color(color, 0x00ffffff, 1) || broken(compare_color(color, 0x000000ff, 1)),
3299 "Got unexpected color 0x%08x.\n", color);
3301 IDirectDrawSurface_Release(rt);
3302 destroy_viewport(device, viewport);
3303 destroy_material(white);
3304 destroy_material(red);
3305 destroy_material(green);
3306 destroy_material(blue);
3307 IDirect3DDevice_Release(device);
3308 IDirectDraw_Release(ddraw);
3309 DestroyWindow(window);
3312 static struct
3314 BOOL received;
3315 IDirectDraw *ddraw;
3316 HWND window;
3317 DWORD coop_level;
3318 } activateapp_testdata;
3320 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
3322 if (message == WM_ACTIVATEAPP)
3324 if (activateapp_testdata.ddraw)
3326 HRESULT hr;
3327 activateapp_testdata.received = FALSE;
3328 hr = IDirectDraw_SetCooperativeLevel(activateapp_testdata.ddraw,
3329 activateapp_testdata.window, activateapp_testdata.coop_level);
3330 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
3331 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
3333 activateapp_testdata.received = TRUE;
3336 return DefWindowProcA(hwnd, message, wparam, lparam);
3339 static void test_coop_level_activateapp(void)
3341 IDirectDraw *ddraw;
3342 HRESULT hr;
3343 HWND window;
3344 WNDCLASSA wc = {0};
3345 DDSURFACEDESC ddsd;
3346 IDirectDrawSurface *surface;
3348 ddraw = create_ddraw();
3349 ok(!!ddraw, "Failed to create a ddraw object.\n");
3351 wc.lpfnWndProc = activateapp_test_proc;
3352 wc.lpszClassName = "ddraw_test_wndproc_wc";
3353 ok(RegisterClassA(&wc), "Failed to register window class.\n");
3355 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
3356 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
3358 /* Exclusive with window already active. */
3359 SetForegroundWindow(window);
3360 activateapp_testdata.received = FALSE;
3361 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3362 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3363 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
3364 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3365 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3367 /* Exclusive with window not active. */
3368 SetForegroundWindow(GetDesktopWindow());
3369 activateapp_testdata.received = FALSE;
3370 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3371 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3372 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3373 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3374 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3376 /* Normal with window not active, then exclusive with the same window. */
3377 SetForegroundWindow(GetDesktopWindow());
3378 activateapp_testdata.received = FALSE;
3379 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3380 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3381 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
3382 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3383 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3384 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3385 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3386 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3388 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
3389 SetForegroundWindow(GetDesktopWindow());
3390 activateapp_testdata.received = FALSE;
3391 activateapp_testdata.ddraw = ddraw;
3392 activateapp_testdata.window = window;
3393 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
3394 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3395 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3396 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3397 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3398 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3400 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
3401 * succeeding. Another switch to exclusive and back to normal is needed to release the
3402 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
3403 * WM_ACTIVATEAPP messages. */
3404 activateapp_testdata.ddraw = NULL;
3405 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3406 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3407 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3408 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3410 /* Setting DDSCL_NORMAL with recursive invocation. */
3411 SetForegroundWindow(GetDesktopWindow());
3412 activateapp_testdata.received = FALSE;
3413 activateapp_testdata.ddraw = ddraw;
3414 activateapp_testdata.window = window;
3415 activateapp_testdata.coop_level = DDSCL_NORMAL;
3416 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3417 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3418 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
3420 /* DDraw is in exlusive mode now. */
3421 memset(&ddsd, 0, sizeof(ddsd));
3422 ddsd.dwSize = sizeof(ddsd);
3423 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
3424 ddsd.dwBackBufferCount = 1;
3425 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
3426 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3427 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
3428 IDirectDrawSurface_Release(surface);
3430 /* Recover again, just to be sure. */
3431 activateapp_testdata.ddraw = NULL;
3432 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3433 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3434 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3435 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3437 DestroyWindow(window);
3438 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3439 IDirectDraw_Release(ddraw);
3442 struct format_support_check
3444 const DDPIXELFORMAT *format;
3445 BOOL supported;
3448 static HRESULT WINAPI test_unsupported_formats_cb(DDSURFACEDESC *desc, void *ctx)
3450 struct format_support_check *format = ctx;
3452 if (!memcmp(format->format, &desc->ddpfPixelFormat, sizeof(*format->format)))
3454 format->supported = TRUE;
3455 return DDENUMRET_CANCEL;
3458 return DDENUMRET_OK;
3461 static void test_unsupported_formats(void)
3463 HRESULT hr;
3464 BOOL expect_success;
3465 HWND window;
3466 IDirectDraw *ddraw;
3467 IDirect3DDevice *device;
3468 IDirectDrawSurface *surface;
3469 DDSURFACEDESC ddsd;
3470 unsigned int i, j;
3471 DWORD expected_caps;
3472 static const struct
3474 const char *name;
3475 DDPIXELFORMAT fmt;
3477 formats[] =
3480 "D3DFMT_A8R8G8B8",
3482 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
3483 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
3487 "D3DFMT_P8",
3489 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
3490 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
3494 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
3496 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3497 0, 0, 640, 480, 0, 0, 0, 0);
3498 ddraw = create_ddraw();
3499 ok(!!ddraw, "Failed to create a ddraw object.\n");
3500 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3502 skip("Failed to create a 3D device, skipping test.\n");
3503 IDirectDraw_Release(ddraw);
3504 DestroyWindow(window);
3505 return;
3508 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
3510 struct format_support_check check = {&formats[i].fmt, FALSE};
3511 hr = IDirect3DDevice_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
3512 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
3514 for (j = 0; j < sizeof(caps) / sizeof(*caps); j++)
3516 memset(&ddsd, 0, sizeof(ddsd));
3517 ddsd.dwSize = sizeof(ddsd);
3518 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
3519 ddsd.ddpfPixelFormat = formats[i].fmt;
3520 ddsd.dwWidth = 4;
3521 ddsd.dwHeight = 4;
3522 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
3524 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
3525 expect_success = FALSE;
3526 else
3527 expect_success = TRUE;
3529 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3530 ok(SUCCEEDED(hr) == expect_success,
3531 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
3532 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
3533 if (FAILED(hr))
3534 continue;
3536 memset(&ddsd, 0, sizeof(ddsd));
3537 ddsd.dwSize = sizeof(ddsd);
3538 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &ddsd);
3539 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3541 if (caps[j] & DDSCAPS_VIDEOMEMORY)
3542 expected_caps = DDSCAPS_VIDEOMEMORY;
3543 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
3544 expected_caps = DDSCAPS_SYSTEMMEMORY;
3545 else if (check.supported)
3546 expected_caps = DDSCAPS_VIDEOMEMORY;
3547 else
3548 expected_caps = DDSCAPS_SYSTEMMEMORY;
3550 ok(ddsd.ddsCaps.dwCaps & expected_caps,
3551 "Expected capability %#x, format %s, input cap %#x.\n",
3552 expected_caps, formats[i].name, caps[j]);
3554 IDirectDrawSurface_Release(surface);
3558 IDirect3DDevice_Release(device);
3559 IDirectDraw_Release(ddraw);
3560 DestroyWindow(window);
3563 static void test_rt_caps(void)
3565 PALETTEENTRY palette_entries[256];
3566 IDirectDrawPalette *palette;
3567 IDirect3DDevice *device;
3568 IDirectDraw *ddraw;
3569 DWORD z_depth = 0;
3570 unsigned int i;
3571 ULONG refcount;
3572 HWND window;
3573 HRESULT hr;
3575 static const DDPIXELFORMAT p8_fmt =
3577 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
3578 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
3581 static const struct
3583 const DDPIXELFORMAT *pf;
3584 DWORD caps_in;
3585 DWORD caps_out;
3586 HRESULT create_device_hr;
3587 BOOL create_may_fail;
3589 test_data[] =
3592 NULL,
3593 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
3594 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3595 D3D_OK,
3596 FALSE,
3599 NULL,
3600 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
3601 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3602 D3D_OK,
3603 FALSE,
3606 NULL,
3607 DDSCAPS_OFFSCREENPLAIN,
3608 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3609 DDERR_INVALIDCAPS,
3610 FALSE,
3613 NULL,
3614 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3615 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3616 D3DERR_SURFACENOTINVIDMEM,
3617 FALSE,
3620 NULL,
3621 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3622 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3623 DDERR_INVALIDCAPS,
3624 FALSE,
3627 NULL,
3628 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
3629 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3630 D3D_OK,
3631 FALSE,
3634 NULL,
3635 DDSCAPS_3DDEVICE,
3636 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3637 D3D_OK,
3638 FALSE,
3641 NULL,
3643 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3644 DDERR_INVALIDCAPS,
3645 FALSE,
3648 NULL,
3649 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3650 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3651 D3DERR_SURFACENOTINVIDMEM,
3652 FALSE,
3655 NULL,
3656 DDSCAPS_SYSTEMMEMORY,
3657 DDSCAPS_SYSTEMMEMORY,
3658 DDERR_INVALIDCAPS,
3659 FALSE,
3662 &p8_fmt,
3664 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3665 DDERR_INVALIDCAPS,
3666 FALSE,
3669 &p8_fmt,
3670 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
3671 ~0U /* AMD r200 */ ,
3672 DDERR_NOPALETTEATTACHED,
3673 FALSE,
3676 &p8_fmt,
3677 DDSCAPS_OFFSCREENPLAIN,
3678 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
3679 DDERR_INVALIDCAPS,
3680 FALSE,
3683 &p8_fmt,
3684 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3685 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
3686 DDERR_NOPALETTEATTACHED,
3687 FALSE,
3690 &p8_fmt,
3691 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3692 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3693 DDERR_INVALIDCAPS,
3694 FALSE,
3697 NULL,
3698 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
3699 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
3700 DDERR_INVALIDCAPS,
3701 TRUE /* AMD Evergreen */,
3704 NULL,
3705 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
3706 ~0U /* AMD Evergreen */,
3707 DDERR_INVALIDCAPS,
3708 FALSE,
3711 NULL,
3712 DDSCAPS_ZBUFFER,
3713 ~0U /* AMD Evergreen */,
3714 DDERR_INVALIDCAPS,
3715 FALSE,
3718 NULL,
3719 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
3720 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
3721 DDERR_INVALIDCAPS,
3722 TRUE /* Nvidia Kepler */,
3725 NULL,
3726 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
3727 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
3728 DDERR_INVALIDCAPS,
3729 TRUE /* Nvidia Kepler */,
3733 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3734 0, 0, 640, 480, 0, 0, 0, 0);
3735 ddraw = create_ddraw();
3736 ok(!!ddraw, "Failed to create a ddraw object.\n");
3737 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
3739 skip("Failed to create a 3D device, skipping test.\n");
3740 IDirectDraw_Release(ddraw);
3741 DestroyWindow(window);
3742 return;
3744 z_depth = get_device_z_depth(device);
3745 ok(!!z_depth, "Failed to get device z depth.\n");
3746 IDirect3DDevice_Release(device);
3748 memset(palette_entries, 0, sizeof(palette_entries));
3749 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
3750 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
3752 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
3754 IDirectDrawSurface *surface;
3755 DDSURFACEDESC surface_desc;
3756 IDirect3DDevice *device;
3758 memset(&surface_desc, 0, sizeof(surface_desc));
3759 surface_desc.dwSize = sizeof(surface_desc);
3760 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
3761 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
3762 if (test_data[i].pf)
3764 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
3765 surface_desc.ddpfPixelFormat = *test_data[i].pf;
3767 if (test_data[i].caps_in & DDSCAPS_ZBUFFER)
3769 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
3770 U2(surface_desc).dwZBufferBitDepth = z_depth;
3772 surface_desc.dwWidth = 640;
3773 surface_desc.dwHeight = 480;
3774 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
3775 ok(SUCCEEDED(hr) || broken(test_data[i].create_may_fail),
3776 "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
3777 i, test_data[i].caps_in, hr);
3778 if (FAILED(hr))
3779 continue;
3781 memset(&surface_desc, 0, sizeof(surface_desc));
3782 surface_desc.dwSize = sizeof(surface_desc);
3783 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
3784 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
3785 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
3786 "Test %u: Got unexpected caps %#x, expected %#x.\n",
3787 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
3789 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
3790 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
3791 i, hr, test_data[i].create_device_hr);
3792 if (hr == DDERR_NOPALETTEATTACHED)
3794 hr = IDirectDrawSurface_SetPalette(surface, palette);
3795 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
3796 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DHALDevice, (void **)&device);
3797 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
3798 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
3799 else
3800 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
3802 if (SUCCEEDED(hr))
3804 refcount = IDirect3DDevice_Release(device);
3805 ok(refcount == 1, "Test %u: Got unexpected refcount %u.\n", i, refcount);
3808 refcount = IDirectDrawSurface_Release(surface);
3809 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
3812 IDirectDrawPalette_Release(palette);
3813 refcount = IDirectDraw_Release(ddraw);
3814 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
3815 DestroyWindow(window);
3818 static void test_primary_caps(void)
3820 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
3821 IDirectDrawSurface *surface;
3822 DDSURFACEDESC surface_desc;
3823 IDirectDraw *ddraw;
3824 unsigned int i;
3825 ULONG refcount;
3826 HWND window;
3827 HRESULT hr;
3829 static const struct
3831 DWORD coop_level;
3832 DWORD caps_in;
3833 DWORD back_buffer_count;
3834 HRESULT hr;
3835 DWORD caps_out;
3837 test_data[] =
3840 DDSCL_NORMAL,
3841 DDSCAPS_PRIMARYSURFACE,
3842 ~0u,
3843 DD_OK,
3844 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
3847 DDSCL_NORMAL,
3848 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
3849 ~0u,
3850 DDERR_INVALIDCAPS,
3851 ~0u,
3854 DDSCL_NORMAL,
3855 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
3856 ~0u,
3857 DD_OK,
3858 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
3861 DDSCL_NORMAL,
3862 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
3863 ~0u,
3864 DDERR_INVALIDCAPS,
3865 ~0u,
3868 DDSCL_NORMAL,
3869 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
3870 ~0u,
3871 DDERR_INVALIDCAPS,
3872 ~0u,
3875 DDSCL_NORMAL,
3876 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
3877 ~0u,
3878 DDERR_INVALIDCAPS,
3879 ~0u,
3882 DDSCL_NORMAL,
3883 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3884 ~0u,
3885 DDERR_INVALIDCAPS,
3886 ~0u,
3889 DDSCL_NORMAL,
3890 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3892 DDERR_INVALIDCAPS,
3893 ~0u,
3896 DDSCL_NORMAL,
3897 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3899 DDERR_NOEXCLUSIVEMODE,
3900 ~0u,
3903 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3904 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3906 DDERR_INVALIDCAPS,
3907 ~0u,
3910 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3911 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
3913 DD_OK,
3914 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
3917 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3918 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
3920 DDERR_INVALIDCAPS,
3921 ~0u,
3924 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
3925 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
3927 DDERR_INVALIDCAPS,
3928 ~0u,
3932 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3933 0, 0, 640, 480, 0, 0, 0, 0);
3934 ddraw = create_ddraw();
3935 ok(!!ddraw, "Failed to create a ddraw object.\n");
3937 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
3939 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
3940 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3942 memset(&surface_desc, 0, sizeof(surface_desc));
3943 surface_desc.dwSize = sizeof(surface_desc);
3944 surface_desc.dwFlags = DDSD_CAPS;
3945 if (test_data[i].back_buffer_count != ~0u)
3946 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
3947 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
3948 surface_desc.dwBackBufferCount = test_data[i].back_buffer_count;
3949 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
3950 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
3951 if (FAILED(hr))
3952 continue;
3954 memset(&surface_desc, 0, sizeof(surface_desc));
3955 surface_desc.dwSize = sizeof(surface_desc);
3956 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
3957 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
3958 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
3959 "Test %u: Got unexpected caps %#x, expected %#x.\n",
3960 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
3962 IDirectDrawSurface_Release(surface);
3965 refcount = IDirectDraw_Release(ddraw);
3966 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
3967 DestroyWindow(window);
3970 static void test_surface_lock(void)
3972 IDirectDraw *ddraw;
3973 IDirectDrawSurface *surface;
3974 IDirect3DDevice *device;
3975 HRESULT hr;
3976 HWND window;
3977 unsigned int i;
3978 DDSURFACEDESC ddsd;
3979 ULONG refcount;
3980 DWORD z_depth = 0;
3981 static const struct
3983 DWORD caps;
3984 const char *name;
3986 tests[] =
3989 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
3990 "videomemory offscreenplain"
3993 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
3994 "systemmemory offscreenplain"
3997 DDSCAPS_PRIMARYSURFACE,
3998 "primary"
4001 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
4002 "videomemory texture"
4005 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
4006 "systemmemory texture"
4009 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4010 "render target"
4013 DDSCAPS_ZBUFFER,
4014 "Z buffer"
4018 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4019 0, 0, 640, 480, 0, 0, 0, 0);
4020 ddraw = create_ddraw();
4021 ok(!!ddraw, "Failed to create a ddraw object.\n");
4022 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
4024 skip("Failed to create a 3D device, skipping test.\n");
4025 IDirectDraw_Release(ddraw);
4026 DestroyWindow(window);
4027 return;
4029 z_depth = get_device_z_depth(device);
4030 ok(!!z_depth, "Failed to get device z depth.\n");
4031 IDirect3DDevice_Release(device);
4033 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4035 memset(&ddsd, 0, sizeof(ddsd));
4036 ddsd.dwSize = sizeof(ddsd);
4037 ddsd.dwFlags = DDSD_CAPS;
4038 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
4040 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
4041 ddsd.dwWidth = 64;
4042 ddsd.dwHeight = 64;
4044 if (tests[i].caps & DDSCAPS_ZBUFFER)
4046 ddsd.dwFlags |= DDSD_ZBUFFERBITDEPTH;
4047 U2(ddsd).dwZBufferBitDepth = z_depth;
4049 ddsd.ddsCaps.dwCaps = tests[i].caps;
4051 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
4052 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
4054 memset(&ddsd, 0, sizeof(ddsd));
4055 ddsd.dwSize = sizeof(ddsd);
4056 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4057 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
4058 if (SUCCEEDED(hr))
4060 hr = IDirectDrawSurface_Unlock(surface, NULL);
4061 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
4064 IDirectDrawSurface_Release(surface);
4067 refcount = IDirectDraw_Release(ddraw);
4068 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4069 DestroyWindow(window);
4072 static void test_surface_discard(void)
4074 IDirectDraw *ddraw;
4075 IDirect3DDevice *device;
4076 HRESULT hr;
4077 HWND window;
4078 DDSURFACEDESC ddsd;
4079 IDirectDrawSurface *surface, *target;
4080 void *addr;
4081 static const struct
4083 DWORD caps;
4084 BOOL discard;
4086 tests[] =
4088 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, TRUE},
4089 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, FALSE},
4090 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, TRUE},
4091 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, FALSE},
4093 unsigned int i;
4095 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4096 0, 0, 640, 480, 0, 0, 0, 0);
4097 ddraw = create_ddraw();
4098 ok(!!ddraw, "Failed to create a ddraw object.\n");
4099 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
4101 skip("Failed to create a 3D device, skipping test.\n");
4102 IDirectDraw_Release(ddraw);
4103 DestroyWindow(window);
4104 return;
4107 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&target);
4108 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4110 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4112 BOOL discarded;
4114 memset(&ddsd, 0, sizeof(ddsd));
4115 ddsd.dwSize = sizeof(ddsd);
4116 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4117 ddsd.ddsCaps.dwCaps = tests[i].caps;
4118 ddsd.dwWidth = 64;
4119 ddsd.dwHeight = 64;
4120 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
4121 if (FAILED(hr))
4123 skip("Failed to create surface, skipping.\n");
4124 continue;
4127 memset(&ddsd, 0, sizeof(ddsd));
4128 ddsd.dwSize = sizeof(ddsd);
4129 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
4130 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4131 addr = ddsd.lpSurface;
4132 hr = IDirectDrawSurface_Unlock(surface, NULL);
4133 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4135 memset(&ddsd, 0, sizeof(ddsd));
4136 ddsd.dwSize = sizeof(ddsd);
4137 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL);
4138 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4139 discarded = ddsd.lpSurface != addr;
4140 hr = IDirectDrawSurface_Unlock(surface, NULL);
4141 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4143 hr = IDirectDrawSurface_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
4144 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
4146 memset(&ddsd, 0, sizeof(ddsd));
4147 ddsd.dwSize = sizeof(ddsd);
4148 hr = IDirectDrawSurface_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS | DDLOCK_WAIT, NULL);
4149 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
4150 discarded |= ddsd.lpSurface != addr;
4151 hr = IDirectDrawSurface_Unlock(surface, NULL);
4152 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
4154 IDirectDrawSurface_Release(surface);
4156 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
4157 * AMD r500, evergreen). Windows XP, at least on AMD r200, never changes the pointer. */
4158 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
4161 IDirectDrawSurface_Release(target);
4162 IDirect3DDevice_Release(device);
4163 IDirectDraw_Release(ddraw);
4164 DestroyWindow(window);
4167 static void test_flip(void)
4169 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
4170 IDirectDrawSurface *primary, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
4171 DDSCAPS caps = {DDSCAPS_FLIP};
4172 DDSURFACEDESC surface_desc;
4173 BOOL sysmem_primary;
4174 IDirectDraw *ddraw;
4175 D3DCOLOR color;
4176 ULONG refcount;
4177 HWND window;
4178 DDBLTFX fx;
4179 HRESULT hr;
4181 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4182 0, 0, 640, 480, 0, 0, 0, 0);
4183 ddraw = create_ddraw();
4184 ok(!!ddraw, "Failed to create a ddraw object.\n");
4186 hr = set_display_mode(ddraw, 640, 480);
4187 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
4188 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4189 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4191 memset(&surface_desc, 0, sizeof(surface_desc));
4192 surface_desc.dwSize = sizeof(surface_desc);
4193 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4194 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4195 surface_desc.dwBackBufferCount = 3;
4196 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
4197 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4199 memset(&surface_desc, 0, sizeof(surface_desc));
4200 surface_desc.dwSize = sizeof(surface_desc);
4201 hr = IDirectDrawSurface_GetSurfaceDesc(primary, &surface_desc);
4202 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4203 ok((surface_desc.ddsCaps.dwCaps & ~placement)
4204 == (DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX),
4205 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4206 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
4208 hr = IDirectDrawSurface_GetAttachedSurface(primary, &caps, &backbuffer1);
4209 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4210 memset(&surface_desc, 0, sizeof(surface_desc));
4211 surface_desc.dwSize = sizeof(surface_desc);
4212 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer1, &surface_desc);
4213 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4214 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
4215 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_BACKBUFFER),
4216 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4218 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
4219 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4220 memset(&surface_desc, 0, sizeof(surface_desc));
4221 surface_desc.dwSize = sizeof(surface_desc);
4222 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer2, &surface_desc);
4223 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4224 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
4225 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
4226 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4228 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
4229 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4230 memset(&surface_desc, 0, sizeof(surface_desc));
4231 surface_desc.dwSize = sizeof(surface_desc);
4232 hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer3, &surface_desc);
4233 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4234 ok(!surface_desc.dwBackBufferCount, "Got unexpected back buffer count %u.\n", surface_desc.dwBackBufferCount);
4235 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
4236 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
4238 hr = IDirectDrawSurface_GetAttachedSurface(backbuffer3, &caps, &surface);
4239 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4240 ok(surface == primary, "Got unexpected surface %p, expected %p.\n", surface, primary);
4241 IDirectDrawSurface_Release(surface);
4243 memset(&surface_desc, 0, sizeof(surface_desc));
4244 surface_desc.dwSize = sizeof(surface_desc);
4245 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4246 surface_desc.ddsCaps.dwCaps = 0;
4247 surface_desc.dwWidth = 640;
4248 surface_desc.dwHeight = 480;
4249 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
4250 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4251 hr = IDirectDrawSurface_Flip(primary, surface, DDFLIP_WAIT);
4252 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4253 IDirectDrawSurface_Release(surface);
4255 hr = IDirectDrawSurface_Flip(primary, primary, DDFLIP_WAIT);
4256 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4257 hr = IDirectDrawSurface_Flip(backbuffer1, NULL, DDFLIP_WAIT);
4258 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4259 hr = IDirectDrawSurface_Flip(backbuffer2, NULL, DDFLIP_WAIT);
4260 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4261 hr = IDirectDrawSurface_Flip(backbuffer3, NULL, DDFLIP_WAIT);
4262 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
4264 memset(&fx, 0, sizeof(fx));
4265 fx.dwSize = sizeof(fx);
4266 U5(fx).dwFillColor = 0xffff0000;
4267 hr = IDirectDrawSurface_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4268 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4269 U5(fx).dwFillColor = 0xff00ff00;
4270 hr = IDirectDrawSurface_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4271 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4272 U5(fx).dwFillColor = 0xff0000ff;
4273 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4274 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4276 hr = IDirectDrawSurface_Flip(primary, NULL, DDFLIP_WAIT);
4277 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4278 color = get_surface_color(backbuffer1, 320, 240);
4279 /* The testbot seems to just copy the contents of one surface to all the
4280 * others, instead of properly flipping. */
4281 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
4282 "Got unexpected color 0x%08x.\n", color);
4283 color = get_surface_color(backbuffer2, 320, 240);
4284 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
4285 U5(fx).dwFillColor = 0xffff0000;
4286 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4287 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4289 hr = IDirectDrawSurface_Flip(primary, NULL, DDFLIP_WAIT);
4290 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4291 color = get_surface_color(backbuffer1, 320, 240);
4292 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
4293 "Got unexpected color 0x%08x.\n", color);
4294 color = get_surface_color(backbuffer2, 320, 240);
4295 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
4296 U5(fx).dwFillColor = 0xff00ff00;
4297 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4298 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4300 hr = IDirectDrawSurface_Flip(primary, NULL, DDFLIP_WAIT);
4301 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4302 color = get_surface_color(backbuffer1, 320, 240);
4303 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
4304 "Got unexpected color 0x%08x.\n", color);
4305 color = get_surface_color(backbuffer2, 320, 240);
4306 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
4307 U5(fx).dwFillColor = 0xff0000ff;
4308 hr = IDirectDrawSurface_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4309 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4311 hr = IDirectDrawSurface_Flip(primary, backbuffer1, DDFLIP_WAIT);
4312 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4313 color = get_surface_color(backbuffer2, 320, 240);
4314 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
4315 "Got unexpected color 0x%08x.\n", color);
4316 color = get_surface_color(backbuffer3, 320, 240);
4317 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
4318 U5(fx).dwFillColor = 0xffff0000;
4319 hr = IDirectDrawSurface_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4320 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4322 hr = IDirectDrawSurface_Flip(primary, backbuffer2, DDFLIP_WAIT);
4323 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4324 color = get_surface_color(backbuffer1, 320, 240);
4325 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
4326 color = get_surface_color(backbuffer3, 320, 240);
4327 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
4328 "Got unexpected color 0x%08x.\n", color);
4329 U5(fx).dwFillColor = 0xff00ff00;
4330 hr = IDirectDrawSurface_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4331 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
4333 hr = IDirectDrawSurface_Flip(primary, backbuffer3, DDFLIP_WAIT);
4334 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
4335 color = get_surface_color(backbuffer1, 320, 240);
4336 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
4337 "Got unexpected color 0x%08x.\n", color);
4338 color = get_surface_color(backbuffer2, 320, 240);
4339 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
4341 IDirectDrawSurface_Release(backbuffer3);
4342 IDirectDrawSurface_Release(backbuffer2);
4343 IDirectDrawSurface_Release(backbuffer1);
4344 IDirectDrawSurface_Release(primary);
4345 refcount = IDirectDraw_Release(ddraw);
4346 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
4347 DestroyWindow(window);
4350 static void test_sysmem_overlay(void)
4352 IDirectDraw *ddraw;
4353 HWND window;
4354 HRESULT hr;
4355 DDSURFACEDESC ddsd;
4356 IDirectDrawSurface *surface;
4357 ULONG ref;
4359 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4360 0, 0, 640, 480, 0, 0, 0, 0);
4361 ddraw = create_ddraw();
4362 ok(!!ddraw, "Failed to create a ddraw object.\n");
4364 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4365 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4367 memset(&ddsd, 0, sizeof(ddsd));
4368 ddsd.dwSize = sizeof(ddsd);
4369 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
4370 ddsd.dwWidth = 16;
4371 ddsd.dwHeight = 16;
4372 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
4373 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
4374 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
4375 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
4376 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
4377 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
4378 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
4379 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
4380 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
4382 ref = IDirectDraw_Release(ddraw);
4383 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
4384 DestroyWindow(window);
4387 static void test_primary_palette(void)
4389 DDSCAPS surface_caps = {DDSCAPS_FLIP};
4390 IDirectDrawSurface *primary, *backbuffer;
4391 PALETTEENTRY palette_entries[256];
4392 IDirectDrawPalette *palette, *tmp;
4393 DDSURFACEDESC surface_desc;
4394 IDirectDraw *ddraw;
4395 DWORD palette_caps;
4396 ULONG refcount;
4397 HWND window;
4398 HRESULT hr;
4400 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4401 0, 0, 640, 480, 0, 0, 0, 0);
4402 ddraw = create_ddraw();
4403 ok(!!ddraw, "Failed to create a ddraw object.\n");
4404 if (FAILED(IDirectDraw_SetDisplayMode(ddraw, 640, 480, 8)))
4406 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
4407 IDirectDraw_Release(ddraw);
4408 DestroyWindow(window);
4409 return;
4411 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4412 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4414 memset(&surface_desc, 0, sizeof(surface_desc));
4415 surface_desc.dwSize = sizeof(surface_desc);
4416 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4417 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4418 surface_desc.dwBackBufferCount = 1;
4419 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
4420 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4421 hr = IDirectDrawSurface_GetAttachedSurface(primary, &surface_caps, &backbuffer);
4422 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
4424 memset(palette_entries, 0, sizeof(palette_entries));
4425 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
4426 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
4427 refcount = get_refcount((IUnknown *)palette);
4428 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4430 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
4431 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
4432 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
4434 hr = IDirectDrawSurface_SetPalette(primary, palette);
4435 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
4437 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
4438 * and is generally somewhat broken with respect to 8 bpp / palette
4439 * handling. */
4440 if (SUCCEEDED(IDirectDrawSurface_GetPalette(backbuffer, &tmp)))
4442 win_skip("Broken palette handling detected, skipping tests.\n");
4443 IDirectDrawPalette_Release(tmp);
4444 IDirectDrawPalette_Release(palette);
4445 /* The Windows 8 testbot keeps extra references to the primary and
4446 * backbuffer while in 8 bpp mode. */
4447 hr = IDirectDraw_RestoreDisplayMode(ddraw);
4448 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
4449 goto done;
4452 refcount = get_refcount((IUnknown *)palette);
4453 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4455 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
4456 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
4457 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
4458 "Got unexpected palette caps %#x.\n", palette_caps);
4460 hr = IDirectDrawSurface_SetPalette(primary, NULL);
4461 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
4462 refcount = get_refcount((IUnknown *)palette);
4463 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4465 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
4466 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
4467 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
4469 hr = IDirectDrawSurface_SetPalette(primary, palette);
4470 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
4471 refcount = get_refcount((IUnknown *)palette);
4472 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4474 hr = IDirectDrawSurface_GetPalette(primary, &tmp);
4475 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
4476 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
4477 IDirectDrawPalette_Release(tmp);
4478 hr = IDirectDrawSurface_GetPalette(backbuffer, &tmp);
4479 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
4481 refcount = IDirectDrawPalette_Release(palette);
4482 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4483 refcount = IDirectDrawPalette_Release(palette);
4484 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4486 /* Note that this only seems to work when the palette is attached to the
4487 * primary surface. When attached to a regular surface, attempting to get
4488 * the palette here will cause an access violation. */
4489 hr = IDirectDrawSurface_GetPalette(primary, &tmp);
4490 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
4492 done:
4493 refcount = IDirectDrawSurface_Release(backbuffer);
4494 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4495 refcount = IDirectDrawSurface_Release(primary);
4496 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4497 refcount = IDirectDraw_Release(ddraw);
4498 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4499 DestroyWindow(window);
4502 static HRESULT WINAPI surface_counter(IDirectDrawSurface *surface, DDSURFACEDESC *desc, void *context)
4504 UINT *surface_count = context;
4506 ++(*surface_count);
4507 IDirectDrawSurface_Release(surface);
4509 return DDENUMRET_OK;
4512 static void test_surface_attachment(void)
4514 IDirectDrawSurface *surface1, *surface2, *surface3, *surface4;
4515 DDSCAPS caps = {DDSCAPS_TEXTURE};
4516 DDSURFACEDESC surface_desc;
4517 IDirectDraw *ddraw;
4518 UINT surface_count;
4519 ULONG refcount;
4520 HWND window;
4521 HRESULT hr;
4523 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4524 0, 0, 640, 480, 0, 0, 0, 0);
4525 ddraw = create_ddraw();
4526 ok(!!ddraw, "Failed to create a ddraw object.\n");
4527 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4528 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4530 memset(&surface_desc, 0, sizeof(surface_desc));
4531 surface_desc.dwSize = sizeof(surface_desc);
4532 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
4533 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
4534 U2(surface_desc).dwMipMapCount = 3;
4535 surface_desc.dwWidth = 128;
4536 surface_desc.dwHeight = 128;
4537 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
4538 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4540 hr = IDirectDrawSurface_GetAttachedSurface(surface1, &caps, &surface2);
4541 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
4542 hr = IDirectDrawSurface_GetAttachedSurface(surface2, &caps, &surface3);
4543 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
4544 hr = IDirectDrawSurface_GetAttachedSurface(surface3, &caps, &surface4);
4545 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
4547 surface_count = 0;
4548 IDirectDrawSurface_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
4549 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
4550 surface_count = 0;
4551 IDirectDrawSurface_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
4552 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
4553 surface_count = 0;
4554 IDirectDrawSurface_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
4555 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
4557 memset(&surface_desc, 0, sizeof(surface_desc));
4558 surface_desc.dwSize = sizeof(surface_desc);
4559 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4560 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
4561 surface_desc.dwWidth = 16;
4562 surface_desc.dwHeight = 16;
4563 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
4564 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4566 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
4567 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4568 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
4569 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4570 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface4);
4571 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4572 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface3);
4573 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4574 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface4);
4575 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4576 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface2);
4577 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4579 IDirectDrawSurface_Release(surface4);
4581 memset(&surface_desc, 0, sizeof(surface_desc));
4582 surface_desc.dwSize = sizeof(surface_desc);
4583 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4584 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
4585 surface_desc.dwWidth = 16;
4586 surface_desc.dwHeight = 16;
4587 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
4588 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4590 if (SUCCEEDED(hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4)))
4592 skip("Running on refrast, skipping some tests.\n");
4593 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface4);
4594 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4596 else
4598 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4599 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
4600 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4601 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface4);
4602 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4603 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface3);
4604 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4605 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface4);
4606 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4607 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface2);
4608 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4611 IDirectDrawSurface_Release(surface4);
4612 IDirectDrawSurface_Release(surface3);
4613 IDirectDrawSurface_Release(surface2);
4614 IDirectDrawSurface_Release(surface1);
4616 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4617 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4619 /* Try a single primary and two offscreen plain surfaces. */
4620 memset(&surface_desc, 0, sizeof(surface_desc));
4621 surface_desc.dwSize = sizeof(surface_desc);
4622 surface_desc.dwFlags = DDSD_CAPS;
4623 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
4624 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
4625 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4627 memset(&surface_desc, 0, sizeof(surface_desc));
4628 surface_desc.dwSize = sizeof(surface_desc);
4629 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4630 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4631 surface_desc.dwWidth = registry_mode.dmPelsWidth;
4632 surface_desc.dwHeight = registry_mode.dmPelsHeight;
4633 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
4634 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4636 memset(&surface_desc, 0, sizeof(surface_desc));
4637 surface_desc.dwSize = sizeof(surface_desc);
4638 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4639 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4640 surface_desc.dwWidth = registry_mode.dmPelsWidth;
4641 surface_desc.dwHeight = registry_mode.dmPelsHeight;
4642 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
4643 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4645 /* This one has a different size. */
4646 memset(&surface_desc, 0, sizeof(surface_desc));
4647 surface_desc.dwSize = sizeof(surface_desc);
4648 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
4649 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
4650 surface_desc.dwWidth = 128;
4651 surface_desc.dwHeight = 128;
4652 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
4653 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4655 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4656 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4657 /* Try the reverse without detaching first. */
4658 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
4659 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
4660 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
4661 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4663 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface1);
4664 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4665 /* Try to detach reversed. */
4666 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
4667 ok(hr == DDERR_CANNOTDETACHSURFACE, "Got unexpected hr %#x.\n", hr);
4668 hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface1);
4669 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4671 hr = IDirectDrawSurface_AddAttachedSurface(surface2, surface3);
4672 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4673 hr = IDirectDrawSurface_DeleteAttachedSurface(surface2, 0, surface3);
4674 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4676 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface4);
4677 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4678 hr = IDirectDrawSurface_AddAttachedSurface(surface4, surface1);
4679 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
4681 IDirectDrawSurface_Release(surface4);
4682 IDirectDrawSurface_Release(surface3);
4683 IDirectDrawSurface_Release(surface2);
4684 IDirectDrawSurface_Release(surface1);
4686 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
4687 memset(&surface_desc, 0, sizeof(surface_desc));
4688 surface_desc.dwSize = sizeof(surface_desc);
4689 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
4690 surface_desc.dwWidth = 64;
4691 surface_desc.dwHeight = 64;
4692 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
4693 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
4694 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
4695 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 16;
4696 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xf800;
4697 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x07e0;
4698 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x001f;
4699 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
4700 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4701 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
4702 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4704 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
4705 surface_desc.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
4706 U1(surface_desc.ddpfPixelFormat).dwZBufferBitDepth = 16;
4707 U3(surface_desc.ddpfPixelFormat).dwZBitMask = 0x0000ffff;
4708 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
4709 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4711 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4712 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4713 refcount = get_refcount((IUnknown *)surface2);
4714 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4715 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4716 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
4718 /* Attaching while already attached to other surface. */
4719 hr = IDirectDrawSurface_AddAttachedSurface(surface3, surface2);
4720 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4721 hr = IDirectDrawSurface_DeleteAttachedSurface(surface3, 0, surface2);
4722 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4723 IDirectDrawSurface_Release(surface3);
4725 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1, 0, surface2);
4726 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
4727 refcount = get_refcount((IUnknown *)surface2);
4728 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
4730 /* Automatic detachment on release. */
4731 hr = IDirectDrawSurface_AddAttachedSurface(surface1, surface2);
4732 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
4733 refcount = get_refcount((IUnknown *)surface2);
4734 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
4735 refcount = IDirectDrawSurface_Release(surface1);
4736 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4737 refcount = IDirectDrawSurface_Release(surface2);
4738 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4739 refcount = IDirectDraw_Release(ddraw);
4740 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
4741 DestroyWindow(window);
4744 static void test_pixel_format(void)
4746 HWND window, window2 = NULL;
4747 HDC hdc, hdc2 = NULL;
4748 HMODULE gl = NULL;
4749 int format, test_format;
4750 PIXELFORMATDESCRIPTOR pfd;
4751 IDirectDraw *ddraw = NULL;
4752 IDirectDrawClipper *clipper = NULL;
4753 DDSURFACEDESC ddsd;
4754 IDirectDrawSurface *primary = NULL;
4755 DDBLTFX fx;
4756 HRESULT hr;
4758 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4759 100, 100, 160, 160, NULL, NULL, NULL, NULL);
4760 if (!window)
4762 skip("Failed to create window\n");
4763 return;
4766 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
4767 100, 100, 160, 160, NULL, NULL, NULL, NULL);
4769 hdc = GetDC(window);
4770 if (!hdc)
4772 skip("Failed to get DC\n");
4773 goto cleanup;
4776 if (window2)
4777 hdc2 = GetDC(window2);
4779 gl = LoadLibraryA("opengl32.dll");
4780 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
4782 format = GetPixelFormat(hdc);
4783 ok(format == 0, "new window has pixel format %d\n", format);
4785 ZeroMemory(&pfd, sizeof(pfd));
4786 pfd.nSize = sizeof(pfd);
4787 pfd.nVersion = 1;
4788 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
4789 pfd.iPixelType = PFD_TYPE_RGBA;
4790 pfd.iLayerType = PFD_MAIN_PLANE;
4791 format = ChoosePixelFormat(hdc, &pfd);
4792 if (format <= 0)
4794 skip("no pixel format available\n");
4795 goto cleanup;
4798 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
4800 skip("failed to set pixel format\n");
4801 goto cleanup;
4804 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
4806 skip("failed to set pixel format on second window\n");
4807 if (hdc2)
4809 ReleaseDC(window2, hdc2);
4810 hdc2 = NULL;
4814 ddraw = create_ddraw();
4815 ok(!!ddraw, "Failed to create a ddraw object.\n");
4817 test_format = GetPixelFormat(hdc);
4818 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4820 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4821 if (FAILED(hr))
4823 skip("Failed to set cooperative level, hr %#x.\n", hr);
4824 goto cleanup;
4827 test_format = GetPixelFormat(hdc);
4828 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4830 if (hdc2)
4832 hr = IDirectDraw_CreateClipper(ddraw, 0, &clipper, NULL);
4833 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
4834 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
4835 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
4837 test_format = GetPixelFormat(hdc);
4838 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4840 test_format = GetPixelFormat(hdc2);
4841 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4844 memset(&ddsd, 0, sizeof(ddsd));
4845 ddsd.dwSize = sizeof(ddsd);
4846 ddsd.dwFlags = DDSD_CAPS;
4847 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
4849 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &primary, NULL);
4850 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
4852 test_format = GetPixelFormat(hdc);
4853 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4855 if (hdc2)
4857 test_format = GetPixelFormat(hdc2);
4858 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4861 if (clipper)
4863 hr = IDirectDrawSurface_SetClipper(primary, clipper);
4864 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
4866 test_format = GetPixelFormat(hdc);
4867 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4869 test_format = GetPixelFormat(hdc2);
4870 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4873 memset(&fx, 0, sizeof(fx));
4874 fx.dwSize = sizeof(fx);
4875 hr = IDirectDrawSurface_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
4876 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
4878 test_format = GetPixelFormat(hdc);
4879 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
4881 if (hdc2)
4883 test_format = GetPixelFormat(hdc2);
4884 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
4887 cleanup:
4888 if (primary) IDirectDrawSurface_Release(primary);
4889 if (clipper) IDirectDrawClipper_Release(clipper);
4890 if (ddraw) IDirectDraw_Release(ddraw);
4891 if (gl) FreeLibrary(gl);
4892 if (hdc) ReleaseDC(window, hdc);
4893 if (hdc2) ReleaseDC(window2, hdc2);
4894 if (window) DestroyWindow(window);
4895 if (window2) DestroyWindow(window2);
4898 static void test_create_surface_pitch(void)
4900 IDirectDrawSurface *surface;
4901 DDSURFACEDESC surface_desc;
4902 IDirectDraw *ddraw;
4903 unsigned int i;
4904 ULONG refcount;
4905 HWND window;
4906 HRESULT hr;
4907 void *mem;
4909 static const struct
4911 DWORD caps;
4912 DWORD flags_in;
4913 DWORD pitch_in;
4914 HRESULT hr;
4915 DWORD flags_out;
4916 DWORD pitch_out32;
4917 DWORD pitch_out64;
4919 test_data[] =
4921 /* 0 */
4922 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
4923 0, 0, DD_OK,
4924 DDSD_PITCH, 0x100, 0x100},
4925 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
4926 DDSD_PITCH, 0x104, DD_OK,
4927 DDSD_PITCH, 0x100, 0x100},
4928 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
4929 DDSD_PITCH, 0x0f8, DD_OK,
4930 DDSD_PITCH, 0x100, 0x100},
4931 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN,
4932 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
4933 0, 0, 0 },
4934 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
4935 0, 0, DD_OK,
4936 DDSD_PITCH, 0x100, 0x0fc},
4937 /* 5 */
4938 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
4939 DDSD_PITCH, 0x104, DD_OK,
4940 DDSD_PITCH, 0x100, 0x0fc},
4941 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
4942 DDSD_PITCH, 0x0f8, DD_OK,
4943 DDSD_PITCH, 0x100, 0x0fc},
4944 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
4945 DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
4946 DDSD_PITCH, 0x100, 0x0fc},
4947 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
4948 DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
4949 0, 0, 0 },
4950 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN,
4951 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDPARAMS,
4952 0, 0, 0 },
4953 /* 10 */
4954 {DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_ALLOCONLOAD,
4955 0, 0, DDERR_INVALIDCAPS,
4956 0, 0, 0 },
4957 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
4958 0, 0, DD_OK,
4959 DDSD_PITCH, 0x100, 0 },
4960 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
4961 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
4962 0, 0, 0 },
4963 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_ALLOCONLOAD,
4964 0, 0, DDERR_INVALIDCAPS,
4965 0, 0, 0 },
4966 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
4967 0, 0, DD_OK,
4968 DDSD_PITCH, 0x100, 0 },
4969 /* 15 */
4970 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_ALLOCONLOAD,
4971 DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDPARAMS,
4972 0, 0, 0 },
4974 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
4976 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4977 0, 0, 640, 480, 0, 0, 0, 0);
4978 ddraw = create_ddraw();
4979 ok(!!ddraw, "Failed to create a ddraw object.\n");
4980 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4981 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4983 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
4985 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
4987 memset(&surface_desc, 0, sizeof(surface_desc));
4988 surface_desc.dwSize = sizeof(surface_desc);
4989 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
4990 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
4991 surface_desc.dwWidth = 63;
4992 surface_desc.dwHeight = 63;
4993 U1(surface_desc).lPitch = test_data[i].pitch_in;
4994 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
4995 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
4996 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
4997 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
4998 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
4999 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5000 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5001 if (test_data[i].flags_in & DDSD_LPSURFACE)
5003 HRESULT expected_hr = SUCCEEDED(test_data[i].hr) ? DDERR_INVALIDPARAMS : test_data[i].hr;
5004 ok(hr == expected_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, expected_hr);
5005 surface_desc.lpSurface = mem;
5006 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5008 if ((test_data[i].caps & DDSCAPS_VIDEOMEMORY) && hr == DDERR_NODIRECTDRAWHW)
5009 continue;
5010 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
5011 if (FAILED(hr))
5012 continue;
5014 memset(&surface_desc, 0, sizeof(surface_desc));
5015 surface_desc.dwSize = sizeof(surface_desc);
5016 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
5017 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5018 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
5019 "Test %u: Got unexpected flags %#x, expected %#x.\n",
5020 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
5021 if (!(test_data[i].caps & DDSCAPS_TEXTURE))
5023 if (is_ddraw64 && test_data[i].pitch_out32 != test_data[i].pitch_out64)
5024 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
5025 "Test %u: Got unexpected pitch %u, expected %u.\n",
5026 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
5027 else
5028 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
5029 "Test %u: Got unexpected pitch %u, expected %u.\n",
5030 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
5032 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
5034 IDirectDrawSurface_Release(surface);
5037 HeapFree(GetProcessHeap(), 0, mem);
5038 refcount = IDirectDraw_Release(ddraw);
5039 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5040 DestroyWindow(window);
5043 static void test_mipmap(void)
5045 IDirectDrawSurface *surface, *surface2;
5046 DDSURFACEDESC surface_desc;
5047 IDirectDraw *ddraw;
5048 unsigned int i;
5049 ULONG refcount;
5050 HWND window;
5051 HRESULT hr;
5052 DDSCAPS caps = {DDSCAPS_COMPLEX};
5053 DDCAPS hal_caps;
5055 static const struct
5057 DWORD flags;
5058 DWORD caps;
5059 DWORD width;
5060 DWORD height;
5061 DWORD mipmap_count_in;
5062 HRESULT hr;
5063 DWORD mipmap_count_out;
5065 tests[] =
5067 {DDSD_MIPMAPCOUNT, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 3, DD_OK, 3},
5068 {DDSD_MIPMAPCOUNT, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 0, DDERR_INVALIDPARAMS, 0},
5069 {0, DDSCAPS_TEXTURE | DDSCAPS_MIPMAP, 128, 32, 0, DD_OK, 1},
5070 {0, DDSCAPS_MIPMAP, 128, 32, 0, DDERR_INVALIDCAPS, 0},
5071 {0, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 128, 32, 0, DD_OK, 6},
5072 {0, DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP, 32, 64, 0, DD_OK, 6},
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(&hal_caps, 0, sizeof(hal_caps));
5083 hal_caps.dwSize = sizeof(hal_caps);
5084 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
5085 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
5086 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
5088 skip("Mipmapped textures not supported, skipping tests.\n");
5089 IDirectDraw_Release(ddraw);
5090 DestroyWindow(window);
5091 return;
5094 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
5096 memset(&surface_desc, 0, sizeof(surface_desc));
5097 surface_desc.dwSize = sizeof(surface_desc);
5098 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | tests[i].flags;
5099 surface_desc.ddsCaps.dwCaps = tests[i].caps;
5100 surface_desc.dwWidth = tests[i].width;
5101 surface_desc.dwHeight = tests[i].height;
5102 if (tests[i].flags & DDSD_MIPMAPCOUNT)
5103 U2(surface_desc).dwMipMapCount = tests[i].mipmap_count_in;
5104 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5105 ok(hr == tests[i].hr, "Test %u: Got unexpected hr %#x.\n", i, hr);
5106 if (FAILED(hr))
5107 continue;
5109 memset(&surface_desc, 0, sizeof(surface_desc));
5110 surface_desc.dwSize = sizeof(surface_desc);
5111 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
5112 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5113 ok(surface_desc.dwFlags & DDSD_MIPMAPCOUNT,
5114 "Test %u: Got unexpected flags %#x.\n", i, surface_desc.dwFlags);
5115 ok(U2(surface_desc).dwMipMapCount == tests[i].mipmap_count_out,
5116 "Test %u: Got unexpected mipmap count %u.\n", i, U2(surface_desc).dwMipMapCount);
5118 if (U2(surface_desc).dwMipMapCount > 1)
5120 hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &surface2);
5121 ok(SUCCEEDED(hr), "Test %u: Failed to get attached surface, hr %#x.\n", i, hr);
5123 memset(&surface_desc, 0, sizeof(surface_desc));
5124 surface_desc.dwSize = sizeof(surface_desc);
5125 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, 0, NULL);
5126 ok(SUCCEEDED(hr), "Test %u: Failed to lock surface, hr %#x.\n", i, hr);
5127 memset(&surface_desc, 0, sizeof(surface_desc));
5128 surface_desc.dwSize = sizeof(surface_desc);
5129 hr = IDirectDrawSurface_Lock(surface2, NULL, &surface_desc, 0, NULL);
5130 ok(SUCCEEDED(hr), "Test %u: Failed to lock surface, hr %#x.\n", i, hr);
5131 IDirectDrawSurface_Unlock(surface2, NULL);
5132 IDirectDrawSurface_Unlock(surface, NULL);
5134 IDirectDrawSurface_Release(surface2);
5137 IDirectDrawSurface_Release(surface);
5140 refcount = IDirectDraw_Release(ddraw);
5141 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5142 DestroyWindow(window);
5145 static void test_palette_complex(void)
5147 IDirectDrawSurface *surface, *mipmap, *tmp;
5148 DDSURFACEDESC surface_desc;
5149 IDirectDraw *ddraw;
5150 IDirectDrawPalette *palette, *palette2, *palette_mipmap;
5151 ULONG refcount;
5152 HWND window;
5153 HRESULT hr;
5154 DDSCAPS caps = {DDSCAPS_COMPLEX};
5155 DDCAPS hal_caps;
5156 PALETTEENTRY palette_entries[256];
5157 unsigned int i;
5158 HDC dc;
5159 RGBQUAD rgbquad;
5160 UINT count;
5162 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5163 0, 0, 640, 480, 0, 0, 0, 0);
5164 ddraw = create_ddraw();
5165 ok(!!ddraw, "Failed to create a ddraw object.\n");
5166 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5167 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5169 memset(&hal_caps, 0, sizeof(hal_caps));
5170 hal_caps.dwSize = sizeof(hal_caps);
5171 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
5172 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
5173 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
5175 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
5176 IDirectDraw_Release(ddraw);
5177 DestroyWindow(window);
5178 return;
5181 memset(&surface_desc, 0, sizeof(surface_desc));
5182 surface_desc.dwSize = sizeof(surface_desc);
5183 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5184 surface_desc.dwWidth = 128;
5185 surface_desc.dwHeight = 128;
5186 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
5187 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5188 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
5189 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
5190 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5191 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5193 memset(palette_entries, 0, sizeof(palette_entries));
5194 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5195 palette_entries, &palette, NULL);
5196 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5198 memset(palette_entries, 0, sizeof(palette_entries));
5199 palette_entries[1].peRed = 0xff;
5200 palette_entries[1].peGreen = 0x80;
5201 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5202 palette_entries, &palette_mipmap, NULL);
5203 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5205 palette2 = (void *)0xdeadbeef;
5206 hr = IDirectDrawSurface_GetPalette(surface, &palette2);
5207 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
5208 ok(!palette2, "Got unexpected palette %p.\n", palette2);
5209 hr = IDirectDrawSurface_SetPalette(surface, palette);
5210 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5211 hr = IDirectDrawSurface_GetPalette(surface, &palette2);
5212 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
5213 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
5214 IDirectDrawPalette_Release(palette2);
5216 mipmap = surface;
5217 IDirectDrawSurface_AddRef(mipmap);
5218 for (i = 0; i < 7; ++i)
5220 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
5221 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
5222 palette2 = (void *)0xdeadbeef;
5223 hr = IDirectDrawSurface_GetPalette(tmp, &palette2);
5224 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
5225 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
5227 hr = IDirectDrawSurface_SetPalette(tmp, palette_mipmap);
5228 ok(SUCCEEDED(hr), "Failed to set palette, i %u, hr %#x.\n", i, hr);
5230 hr = IDirectDrawSurface_GetPalette(tmp, &palette2);
5231 ok(SUCCEEDED(hr), "Failed to get palette, i %u, hr %#x.\n", i, hr);
5232 ok(palette_mipmap == palette2, "Got unexpected palette %p.\n", palette2);
5233 IDirectDrawPalette_Release(palette2);
5235 hr = IDirectDrawSurface_GetDC(tmp, &dc);
5236 ok(SUCCEEDED(hr), "Failed to get DC, i %u, hr %#x.\n", i, hr);
5237 count = GetDIBColorTable(dc, 1, 1, &rgbquad);
5238 ok(count == 1, "Expected count 1, got %u.\n", count);
5239 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x.\n", rgbquad.rgbRed);
5240 ok(rgbquad.rgbGreen == 0x80, "Expected rgbGreen = 0x80, got %#x.\n", rgbquad.rgbGreen);
5241 ok(rgbquad.rgbBlue == 0x0, "Expected rgbBlue = 0x0, got %#x.\n", rgbquad.rgbBlue);
5242 hr = IDirectDrawSurface_ReleaseDC(tmp, dc);
5243 ok(SUCCEEDED(hr), "Failed to release DC, i %u, hr %#x.\n", i, hr);
5245 IDirectDrawSurface_Release(mipmap);
5246 mipmap = tmp;
5249 hr = IDirectDrawSurface_GetAttachedSurface(mipmap, &caps, &tmp);
5250 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
5251 IDirectDrawSurface_Release(mipmap);
5252 refcount = IDirectDrawSurface_Release(surface);
5253 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5254 refcount = IDirectDrawPalette_Release(palette_mipmap);
5255 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5256 refcount = IDirectDrawPalette_Release(palette);
5257 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5259 refcount = IDirectDraw_Release(ddraw);
5260 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5261 DestroyWindow(window);
5264 static void test_p8_rgb_blit(void)
5266 IDirectDrawSurface *src, *dst;
5267 DDSURFACEDESC surface_desc;
5268 IDirectDraw *ddraw;
5269 IDirectDrawPalette *palette;
5270 ULONG refcount;
5271 HWND window;
5272 HRESULT hr;
5273 PALETTEENTRY palette_entries[256];
5274 unsigned int x;
5275 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
5276 static const D3DCOLOR expected[] =
5278 0x00101010, 0x00010101, 0x00020202, 0x00030303,
5279 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
5281 D3DCOLOR color;
5283 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5284 0, 0, 640, 480, 0, 0, 0, 0);
5285 ddraw = create_ddraw();
5286 ok(!!ddraw, "Failed to create a ddraw object.\n");
5287 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5288 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5290 memset(palette_entries, 0, sizeof(palette_entries));
5291 palette_entries[1].peGreen = 0xff;
5292 palette_entries[2].peBlue = 0xff;
5293 palette_entries[3].peFlags = 0xff;
5294 palette_entries[4].peRed = 0xff;
5295 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5296 palette_entries, &palette, NULL);
5297 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5299 memset(&surface_desc, 0, sizeof(surface_desc));
5300 surface_desc.dwSize = sizeof(surface_desc);
5301 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5302 surface_desc.dwWidth = 8;
5303 surface_desc.dwHeight = 1;
5304 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5305 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5306 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
5307 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
5308 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src, NULL);
5309 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5311 memset(&surface_desc, 0, sizeof(surface_desc));
5312 surface_desc.dwSize = sizeof(surface_desc);
5313 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5314 surface_desc.dwWidth = 8;
5315 surface_desc.dwHeight = 1;
5316 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5317 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5318 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
5319 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
5320 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5321 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5322 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
5323 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
5324 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst, NULL);
5325 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5327 memset(&surface_desc, 0, sizeof(surface_desc));
5328 surface_desc.dwSize = sizeof(surface_desc);
5329 hr = IDirectDrawSurface_Lock(src, NULL, &surface_desc, 0, NULL);
5330 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
5331 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
5332 hr = IDirectDrawSurface_Unlock(src, NULL);
5333 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
5335 hr = IDirectDrawSurface_SetPalette(src, palette);
5336 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5337 hr = IDirectDrawSurface_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
5338 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
5339 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
5340 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
5341 "Failed to blit, hr %#x.\n", hr);
5343 if (SUCCEEDED(hr))
5345 for (x = 0; x < sizeof(expected) / sizeof(*expected); x++)
5347 color = get_surface_color(dst, x, 0);
5348 todo_wine ok(compare_color(color, expected[x], 0),
5349 "Pixel %u: Got color %#x, expected %#x.\n",
5350 x, color, expected[x]);
5354 IDirectDrawSurface_Release(src);
5355 IDirectDrawSurface_Release(dst);
5356 IDirectDrawPalette_Release(palette);
5358 refcount = IDirectDraw_Release(ddraw);
5359 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
5360 DestroyWindow(window);
5363 static void test_material(void)
5365 IDirect3DMaterial *background, *material;
5366 IDirect3DExecuteBuffer *execute_buffer;
5367 D3DMATERIALHANDLE mat_handle, tmp;
5368 D3DEXECUTEBUFFERDESC exec_desc;
5369 IDirect3DViewport *viewport;
5370 IDirect3DDevice *device;
5371 IDirectDrawSurface *rt;
5372 IDirectDraw *ddraw;
5373 UINT inst_length;
5374 D3DCOLOR color;
5375 ULONG refcount;
5376 unsigned int i;
5377 HWND window;
5378 HRESULT hr;
5379 BOOL valid;
5380 void *ptr;
5382 static D3DVERTEX quad[] =
5384 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5385 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5386 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5387 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
5389 static const struct
5391 BOOL material;
5392 D3DCOLOR expected_color;
5394 test_data[] =
5396 {TRUE, 0x0000ff00},
5397 {FALSE, 0x00ffffff},
5399 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
5401 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5402 0, 0, 640, 480, 0, 0, 0, 0);
5403 ddraw = create_ddraw();
5404 ok(!!ddraw, "Failed to create a ddraw object.\n");
5405 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
5407 skip("Failed to create a 3D device, skipping test.\n");
5408 DestroyWindow(window);
5409 return;
5412 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
5413 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
5415 background = create_diffuse_material(device, 0.0f, 0.0f, 1.0f, 1.0f);
5416 viewport = create_viewport(device, 0, 0, 640, 480);
5417 viewport_set_background(device, viewport, background);
5419 material = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
5420 hr = IDirect3DMaterial_GetHandle(material, device, &mat_handle);
5421 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
5423 memset(&exec_desc, 0, sizeof(exec_desc));
5424 exec_desc.dwSize = sizeof(exec_desc);
5425 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
5426 exec_desc.dwBufferSize = 1024;
5427 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
5429 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
5430 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
5432 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5434 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5435 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5437 memcpy(exec_desc.lpData, quad, sizeof(quad));
5438 ptr = ((BYTE *)exec_desc.lpData) + sizeof(quad);
5439 emit_set_ls(&ptr, D3DLIGHTSTATE_MATERIAL, test_data[i].material ? mat_handle : 0);
5440 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORMLIGHT, 0, 4);
5441 emit_tquad(&ptr, 0);
5442 emit_end(&ptr);
5443 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5444 inst_length -= sizeof(quad);
5446 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5447 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5449 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER);
5450 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5452 hr = IDirect3DDevice_BeginScene(device);
5453 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
5454 set_execute_data(execute_buffer, 4, sizeof(quad), inst_length);
5455 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5456 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5457 hr = IDirect3DDevice_EndScene(device);
5458 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
5459 color = get_surface_color(rt, 320, 240);
5460 if (test_data[i].material)
5461 todo_wine ok(compare_color(color, test_data[i].expected_color, 1)
5462 /* The Windows 8 testbot appears to return undefined results. */
5463 || broken(TRUE),
5464 "Got unexpected color 0x%08x, test %u.\n", color, i);
5465 else
5466 ok(compare_color(color, test_data[i].expected_color, 1),
5467 "Got unexpected color 0x%08x, test %u.\n", color, i);
5470 destroy_material(material);
5471 material = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
5472 hr = IDirect3DMaterial_GetHandle(material, device, &mat_handle);
5473 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
5475 hr = IDirect3DViewport_SetBackground(viewport, mat_handle);
5476 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
5477 hr = IDirect3DViewport_GetBackground(viewport, &tmp, &valid);
5478 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
5479 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
5480 ok(valid, "Got unexpected valid %#x.\n", valid);
5481 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5482 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5483 color = get_surface_color(rt, 320, 240);
5484 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5486 hr = IDirect3DViewport_SetBackground(viewport, 0);
5487 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
5488 hr = IDirect3DViewport_GetBackground(viewport, &tmp, &valid);
5489 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
5490 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
5491 ok(valid, "Got unexpected valid %#x.\n", valid);
5492 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5493 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5494 color = get_surface_color(rt, 320, 240);
5495 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5497 destroy_viewport(device, viewport);
5498 viewport = create_viewport(device, 0, 0, 640, 480);
5500 hr = IDirect3DViewport_GetBackground(viewport, &tmp, &valid);
5501 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
5502 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
5503 ok(!valid, "Got unexpected valid %#x.\n", valid);
5504 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5505 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5506 color = get_surface_color(rt, 320, 240);
5507 ok(compare_color(color, 0x00000000, 1), "Got unexpected color 0x%08x.\n", color);
5509 IDirect3DExecuteBuffer_Release(execute_buffer);
5510 destroy_viewport(device, viewport);
5511 destroy_material(background);
5512 destroy_material(material);
5513 IDirectDrawSurface_Release(rt);
5514 refcount = IDirect3DDevice_Release(device);
5515 ok(!refcount, "Device has %u references left.\n", refcount);
5516 refcount = IDirectDraw_Release(ddraw);
5517 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
5518 DestroyWindow(window);
5521 static void test_lighting(void)
5523 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
5524 static D3DMATRIX mat =
5526 1.0f, 0.0f, 0.0f, 0.0f,
5527 0.0f, 1.0f, 0.0f, 0.0f,
5528 0.0f, 0.0f, 1.0f, 0.0f,
5529 0.0f, 0.0f, 0.0f, 1.0f,
5531 mat_singular =
5533 1.0f, 0.0f, 1.0f, 0.0f,
5534 0.0f, 1.0f, 0.0f, 0.0f,
5535 1.0f, 0.0f, 1.0f, 0.0f,
5536 0.0f, 0.0f, 0.5f, 1.0f,
5538 mat_transf =
5540 0.0f, 0.0f, 1.0f, 0.0f,
5541 0.0f, 1.0f, 0.0f, 0.0f,
5542 -1.0f, 0.0f, 0.0f, 0.0f,
5543 10.f, 10.0f, 10.0f, 1.0f,
5545 mat_nonaffine =
5547 1.0f, 0.0f, 0.0f, 0.0f,
5548 0.0f, 1.0f, 0.0f, 0.0f,
5549 0.0f, 0.0f, 1.0f, -1.0f,
5550 10.f, 10.0f, 10.0f, 0.0f,
5552 static D3DLVERTEX unlitquad[] =
5554 {{-1.0f}, {-1.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
5555 {{-1.0f}, { 0.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
5556 {{ 0.0f}, { 0.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
5557 {{ 0.0f}, {-1.0f}, {0.1f}, 0, {0xffff0000}, {0}, {0.0f}, {0.0f}},
5559 litquad[] =
5561 {{-1.0f}, { 0.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
5562 {{-1.0f}, { 1.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
5563 {{ 0.0f}, { 1.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
5564 {{ 0.0f}, { 0.0f}, {0.1f}, 0, {0xff00ff00}, {0}, {0.0f}, {0.0f}},
5566 static D3DVERTEX unlitnquad[] =
5568 {{0.0f}, {-1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5569 {{0.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5570 {{1.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5571 {{1.0f}, {-1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5573 litnquad[] =
5575 {{0.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5576 {{0.0f}, { 1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5577 {{1.0f}, { 1.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5578 {{1.0f}, { 0.0f}, {0.1f}, {1.0f}, {1.0f}, {1.0f}, {0.0f}, {0.0f}},
5580 nquad[] =
5582 {{-1.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5583 {{-1.0f}, { 1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5584 {{ 1.0f}, { 1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5585 {{ 1.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5587 rotatedquad[] =
5589 {{-10.0f}, {-11.0f}, {11.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
5590 {{-10.0f}, { -9.0f}, {11.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
5591 {{-10.0f}, { -9.0f}, { 9.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
5592 {{-10.0f}, {-11.0f}, { 9.0f}, {-1.0f}, {0.0f}, {0.0f}, {0.0f}, {0.0f}},
5594 translatedquad[] =
5596 {{-11.0f}, {-11.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5597 {{-11.0f}, { -9.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5598 {{ -9.0f}, { -9.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5599 {{ -9.0f}, {-11.0f}, {-10.0f}, {0.0f}, {0.0f}, {-1.0f}, {0.0f}, {0.0f}},
5601 static const struct
5603 D3DMATRIX *world_matrix;
5604 void *quad;
5605 DWORD expected;
5606 const char *message;
5608 tests[] =
5610 {&mat, nquad, 0x000000ff, "Lit quad with light"},
5611 {&mat_singular, nquad, 0x000000b4, "Lit quad with singular world matrix"},
5612 {&mat_transf, rotatedquad, 0x000000ff, "Lit quad with transformation matrix"},
5613 {&mat_nonaffine, translatedquad, 0x000000ff, "Lit quad with non-affine matrix"},
5616 HWND window;
5617 IDirect3D *d3d;
5618 IDirect3DDevice *device;
5619 IDirectDraw *ddraw;
5620 IDirectDrawSurface *rt;
5621 IDirect3DViewport *viewport;
5622 IDirect3DMaterial *material;
5623 IDirect3DLight *light;
5624 IDirect3DExecuteBuffer *execute_buffer;
5625 D3DEXECUTEBUFFERDESC exec_desc;
5626 D3DMATERIALHANDLE mat_handle;
5627 D3DMATRIXHANDLE world_handle, view_handle, proj_handle;
5628 D3DLIGHT light_desc;
5629 HRESULT hr;
5630 D3DCOLOR color;
5631 void *ptr;
5632 UINT inst_length;
5633 ULONG refcount;
5634 unsigned int i;
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 (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
5642 skip("Failed to create a 3D device, skipping test.\n");
5643 IDirectDraw_Release(ddraw);
5644 DestroyWindow(window);
5645 return;
5648 hr = IDirect3DDevice_GetDirect3D(device, &d3d);
5649 ok(SUCCEEDED(hr), "Failed to get D3D interface, hr %#x.\n", hr);
5651 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
5652 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
5654 viewport = create_viewport(device, 0, 0, 640, 480);
5655 material = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
5656 viewport_set_background(device, viewport, material);
5658 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5659 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5661 hr = IDirect3DDevice_CreateMatrix(device, &world_handle);
5662 ok(hr == D3D_OK, "Creating a matrix object failed, hr %#x.\n", hr);
5663 hr = IDirect3DDevice_SetMatrix(device, world_handle, &mat);
5664 ok(hr == D3D_OK, "Setting a matrix object failed, hr %#x.\n", hr);
5665 hr = IDirect3DDevice_CreateMatrix(device, &view_handle);
5666 ok(hr == D3D_OK, "Creating a matrix object failed, hr %#x.\n", hr);
5667 hr = IDirect3DDevice_SetMatrix(device, view_handle, &mat);
5668 ok(hr == D3D_OK, "Setting a matrix object failed, hr %#x.\n", hr);
5669 hr = IDirect3DDevice_CreateMatrix(device, &proj_handle);
5670 ok(hr == D3D_OK, "Creating a matrix object failed, hr %#x.\n", hr);
5671 hr = IDirect3DDevice_SetMatrix(device, proj_handle, &mat);
5672 ok(hr == D3D_OK, "Setting a matrix object failed, hr %#x.\n", hr);
5674 memset(&exec_desc, 0, sizeof(exec_desc));
5675 exec_desc.dwSize = sizeof(exec_desc);
5676 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
5677 exec_desc.dwBufferSize = 1024;
5678 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
5680 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
5681 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
5683 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5684 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5686 memcpy(exec_desc.lpData, unlitquad, sizeof(unlitquad));
5687 ptr = ((BYTE *)exec_desc.lpData) + sizeof(unlitquad);
5688 emit_set_ts(&ptr, D3DTRANSFORMSTATE_WORLD, world_handle);
5689 emit_set_ts(&ptr, D3DTRANSFORMSTATE_VIEW, view_handle);
5690 emit_set_ts(&ptr, D3DTRANSFORMSTATE_PROJECTION, proj_handle);
5691 emit_set_rs(&ptr, D3DRENDERSTATE_CLIPPING, FALSE);
5692 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, FALSE);
5693 emit_set_rs(&ptr, D3DRENDERSTATE_FOGENABLE, FALSE);
5694 emit_set_rs(&ptr, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
5695 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORM, 0, 4);
5696 emit_tquad_tlist(&ptr, 0);
5697 emit_end(&ptr);
5698 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5699 inst_length -= sizeof(unlitquad);
5701 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5702 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5704 hr = IDirect3DDevice_BeginScene(device);
5705 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
5707 set_execute_data(execute_buffer, 4, sizeof(unlitquad), inst_length);
5708 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5709 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5711 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5712 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5714 memcpy(exec_desc.lpData, litquad, sizeof(litquad));
5715 ptr = ((BYTE *)exec_desc.lpData) + sizeof(litquad);
5716 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORM, 0, 4);
5717 emit_tquad_tlist(&ptr, 0);
5718 emit_end(&ptr);
5719 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5720 inst_length -= sizeof(litquad);
5722 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5723 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5725 set_execute_data(execute_buffer, 4, sizeof(litquad), inst_length);
5726 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5727 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5729 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5730 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5732 memcpy(exec_desc.lpData, unlitnquad, sizeof(unlitnquad));
5733 ptr = ((BYTE *)exec_desc.lpData) + sizeof(unlitnquad);
5734 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORMLIGHT, 0, 4);
5735 emit_tquad_tlist(&ptr, 0);
5736 emit_end(&ptr);
5737 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5738 inst_length -= sizeof(unlitnquad);
5740 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5741 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5743 set_execute_data(execute_buffer, 4, sizeof(unlitnquad), inst_length);
5744 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5745 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5747 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5748 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5750 memcpy(exec_desc.lpData, litnquad, sizeof(litnquad));
5751 ptr = ((BYTE *)exec_desc.lpData) + sizeof(litnquad);
5752 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORMLIGHT, 0, 4);
5753 emit_tquad_tlist(&ptr, 0);
5754 emit_end(&ptr);
5755 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5756 inst_length -= sizeof(litnquad);
5758 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5759 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5761 set_execute_data(execute_buffer, 4, sizeof(litnquad), inst_length);
5762 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5763 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5765 hr = IDirect3DDevice_EndScene(device);
5766 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
5768 color = get_surface_color(rt, 160, 360);
5769 ok(color == 0x00ff0000, "Unlit quad without normals has color 0x%08x.\n", color);
5770 color = get_surface_color(rt, 160, 120);
5771 ok(color == 0x0000ff00, "Lit quad without normals has color 0x%08x.\n", color);
5772 color = get_surface_color(rt, 480, 360);
5773 ok(color == 0x00ffffff, "Unlit quad with normals has color 0x%08x.\n", color);
5774 color = get_surface_color(rt, 480, 120);
5775 ok(color == 0x00ffffff, "Lit quad with normals has color 0x%08x.\n", color);
5777 hr = IDirect3DMaterial_GetHandle(material, device, &mat_handle);
5778 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
5780 hr = IDirect3D_CreateLight(d3d, &light, NULL);
5781 ok(SUCCEEDED(hr), "Failed to create a light object, hr %#x.\n", hr);
5782 memset(&light_desc, 0, sizeof(light_desc));
5783 light_desc.dwSize = sizeof(light_desc);
5784 light_desc.dltType = D3DLIGHT_DIRECTIONAL;
5785 U1(light_desc.dcvColor).r = 0.0f;
5786 U2(light_desc.dcvColor).g = 0.0f;
5787 U3(light_desc.dcvColor).b = 1.0f;
5788 U4(light_desc.dcvColor).a = 1.0f;
5789 U3(light_desc.dvDirection).z = 1.0f;
5790 hr = IDirect3DLight_SetLight(light, &light_desc);
5791 ok(SUCCEEDED(hr), "Failed to set light, hr %#x.\n", hr);
5792 hr = IDirect3DViewport_AddLight(viewport, light);
5793 ok(SUCCEEDED(hr), "Failed to add a light to the viewport, hr %#x.\n", hr);
5795 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
5797 hr = IDirect3DDevice_SetMatrix(device, world_handle, tests[i].world_matrix);
5798 ok(hr == D3D_OK, "Setting a matrix object failed, hr %#x.\n", hr);
5800 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
5801 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
5803 hr = IDirect3DDevice_BeginScene(device);
5804 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
5806 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
5807 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
5809 memcpy(exec_desc.lpData, tests[i].quad, sizeof(nquad));
5810 ptr = ((BYTE *)exec_desc.lpData) + sizeof(nquad);
5811 emit_set_ls(&ptr, D3DLIGHTSTATE_MATERIAL, mat_handle);
5812 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORMLIGHT, 0, 4);
5813 emit_tquad_tlist(&ptr, 0);
5814 emit_end(&ptr);
5815 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
5816 inst_length -= sizeof(nquad);
5818 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
5819 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
5821 set_execute_data(execute_buffer, 4, sizeof(nquad), inst_length);
5822 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
5823 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
5825 hr = IDirect3DDevice_EndScene(device);
5826 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
5828 color = get_surface_color(rt, 320, 240);
5829 todo_wine ok(color == tests[i].expected, "%s has color 0x%08x.\n", tests[i].message, color);
5832 IDirect3DExecuteBuffer_Release(execute_buffer);
5833 IDirect3DDevice_DeleteMatrix(device, world_handle);
5834 IDirect3DDevice_DeleteMatrix(device, view_handle);
5835 IDirect3DDevice_DeleteMatrix(device, proj_handle);
5836 hr = IDirect3DViewport_DeleteLight(viewport, light);
5837 ok(SUCCEEDED(hr), "Failed to remove a light from the viewport, hr %#x.\n", hr);
5838 IDirect3DLight_Release(light);
5839 destroy_material(material);
5840 destroy_viewport(device, viewport);
5841 IDirectDrawSurface_Release(rt);
5842 refcount = IDirect3DDevice_Release(device);
5843 ok(!refcount, "Device has %u references left.\n", refcount);
5844 IDirect3D_Release(d3d);
5845 refcount = IDirectDraw_Release(ddraw);
5846 ok(!refcount, "Ddraw object has %u references left.\n", refcount);
5847 DestroyWindow(window);
5850 static void test_palette_gdi(void)
5852 IDirectDrawSurface *surface, *primary;
5853 DDSURFACEDESC surface_desc;
5854 IDirectDraw *ddraw;
5855 IDirectDrawPalette *palette, *palette2;
5856 ULONG refcount;
5857 HWND window;
5858 HRESULT hr;
5859 PALETTEENTRY palette_entries[256];
5860 UINT i;
5861 HDC dc;
5862 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
5863 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
5864 * not the point of this test. */
5865 static const RGBQUAD expected1[] =
5867 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
5868 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
5870 static const RGBQUAD expected2[] =
5872 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
5873 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
5875 static const RGBQUAD expected3[] =
5877 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
5878 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
5880 HPALETTE ddraw_palette_handle;
5881 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
5882 RGBQUAD rgbquad[255];
5883 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
5885 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5886 0, 0, 640, 480, 0, 0, 0, 0);
5887 ddraw = create_ddraw();
5888 ok(!!ddraw, "Failed to create a ddraw object.\n");
5889 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5890 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5892 memset(&surface_desc, 0, sizeof(surface_desc));
5893 surface_desc.dwSize = sizeof(surface_desc);
5894 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5895 surface_desc.dwWidth = 16;
5896 surface_desc.dwHeight = 16;
5897 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
5898 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
5899 surface_desc.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
5900 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 8;
5901 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5902 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5904 /* Avoid colors from the Windows default palette. */
5905 memset(palette_entries, 0, sizeof(palette_entries));
5906 palette_entries[1].peRed = 0x01;
5907 palette_entries[2].peGreen = 0x02;
5908 palette_entries[3].peBlue = 0x03;
5909 palette_entries[4].peRed = 0x13;
5910 palette_entries[4].peGreen = 0x14;
5911 palette_entries[4].peBlue = 0x15;
5912 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
5913 palette_entries, &palette, NULL);
5914 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5916 /* If there is no palette assigned and the display mode is not 8 bpp, some
5917 * drivers refuse to create a DC while others allow it. If a DC is created,
5918 * the DIB color table is uninitialized and contains random colors. No error
5919 * is generated when trying to read pixels and random garbage is returned.
5921 * The most likely explanation is that if the driver creates a DC, it (or
5922 * the higher-level runtime) uses GetSystemPaletteEntries to find the
5923 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
5924 * contains uninitialized garbage. See comments below for the P8 case. */
5926 hr = IDirectDrawSurface_SetPalette(surface, palette);
5927 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5928 hr = IDirectDrawSurface_GetDC(surface, &dc);
5929 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5930 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
5931 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
5932 "Got unexpected palette %p, expected %p.\n",
5933 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
5935 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5936 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5937 for (i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
5939 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
5940 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5941 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5942 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
5944 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5946 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5947 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5948 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5951 /* Update the palette while the DC is in use. This does not modify the DC. */
5952 palette_entries[4].peRed = 0x23;
5953 palette_entries[4].peGreen = 0x24;
5954 palette_entries[4].peBlue = 0x25;
5955 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
5956 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
5958 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
5959 ok(i == 1, "Expected count 1, got %u.\n", i);
5960 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
5961 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5962 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
5963 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
5965 /* Neither does re-setting the palette. */
5966 hr = IDirectDrawSurface_SetPalette(surface, NULL);
5967 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5968 hr = IDirectDrawSurface_SetPalette(surface, palette);
5969 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
5971 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
5972 ok(i == 1, "Expected count 1, got %u.\n", i);
5973 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
5974 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5975 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
5976 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
5978 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
5979 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
5981 /* Refresh the DC. This updates the palette. */
5982 hr = IDirectDrawSurface_GetDC(surface, &dc);
5983 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
5984 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
5985 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
5986 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
5988 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
5989 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
5990 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
5991 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
5993 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
5995 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
5996 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
5997 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
5999 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
6000 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6002 refcount = IDirectDrawSurface_Release(surface);
6003 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6005 if (FAILED(IDirectDraw_SetDisplayMode(ddraw, 640, 480, 8)))
6007 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
6008 IDirectDrawPalette_Release(palette);
6009 IDirectDraw_Release(ddraw);
6010 DestroyWindow(window);
6011 return;
6013 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
6014 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
6015 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6017 memset(&surface_desc, 0, sizeof(surface_desc));
6018 surface_desc.dwSize = sizeof(surface_desc);
6019 surface_desc.dwFlags = DDSD_CAPS;
6020 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6021 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
6022 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6024 hr = IDirectDrawSurface_SetPalette(primary, palette);
6025 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6026 hr = IDirectDrawSurface_GetDC(primary, &dc);
6027 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6028 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
6029 /* Windows 2000 on the testbot assigns a different palette to the primary. Refrast? */
6030 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE) || broken(TRUE),
6031 "Got unexpected palette %p, expected %p.\n",
6032 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
6033 SelectPalette(dc, ddraw_palette_handle, FALSE);
6035 /* The primary uses the system palette. In exclusive mode, the system palette matches
6036 * the ddraw palette attached to the primary, so the result is what you would expect
6037 * from a regular surface. Tests for the interaction between the ddraw palette and
6038 * the system palette are not included pending an application that depends on this.
6039 * The relation between those causes problems on Windows Vista and newer for games
6040 * like Age of Empires or StarCraft. Don't emulate it without a real need. */
6041 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
6042 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
6043 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
6045 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
6046 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6047 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
6048 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
6050 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
6052 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
6053 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
6054 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
6056 hr = IDirectDrawSurface_ReleaseDC(primary, dc);
6057 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6059 memset(&surface_desc, 0, sizeof(surface_desc));
6060 surface_desc.dwSize = sizeof(surface_desc);
6061 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6062 surface_desc.dwWidth = 16;
6063 surface_desc.dwHeight = 16;
6064 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6065 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6066 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6068 /* Here the offscreen surface appears to use the primary's palette,
6069 * but in all likelihood it is actually the system palette. */
6070 hr = IDirectDrawSurface_GetDC(surface, &dc);
6071 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6072 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
6073 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
6074 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
6076 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
6077 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6078 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
6079 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
6081 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
6083 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
6084 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
6085 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
6087 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
6088 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6090 /* On real hardware a change to the primary surface's palette applies immediately,
6091 * even on device contexts from offscreen surfaces that do not have their own
6092 * palette. On the testbot VMs this is not the case. Don't test this until we
6093 * know of an application that depends on this. */
6095 memset(palette_entries, 0, sizeof(palette_entries));
6096 palette_entries[1].peBlue = 0x40;
6097 palette_entries[2].peRed = 0x40;
6098 palette_entries[3].peGreen = 0x40;
6099 palette_entries[4].peRed = 0x12;
6100 palette_entries[4].peGreen = 0x34;
6101 palette_entries[4].peBlue = 0x56;
6102 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
6103 palette_entries, &palette2, NULL);
6104 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6105 hr = IDirectDrawSurface_SetPalette(surface, palette2);
6106 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6108 /* A palette assigned to the offscreen surface overrides the primary / system
6109 * palette. */
6110 hr = IDirectDrawSurface_GetDC(surface, &dc);
6111 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6112 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
6113 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
6114 for (i = 0; i < sizeof(expected3) / sizeof(*expected3); i++)
6116 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
6117 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
6118 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
6119 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
6121 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
6123 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
6124 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
6125 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
6127 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
6128 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6130 refcount = IDirectDrawSurface_Release(surface);
6131 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6133 /* The Windows 8 testbot keeps extra references to the primary and
6134 * backbuffer while in 8 bpp mode. */
6135 hr = IDirectDraw_RestoreDisplayMode(ddraw);
6136 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
6138 refcount = IDirectDrawSurface_Release(primary);
6139 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6140 refcount = IDirectDrawPalette_Release(palette2);
6141 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6142 refcount = IDirectDrawPalette_Release(palette);
6143 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6144 refcount = IDirectDraw_Release(ddraw);
6145 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6146 DestroyWindow(window);
6149 static void test_palette_alpha(void)
6151 IDirectDrawSurface *surface;
6152 DDSURFACEDESC surface_desc;
6153 IDirectDraw *ddraw;
6154 IDirectDrawPalette *palette;
6155 ULONG refcount;
6156 HWND window;
6157 HRESULT hr;
6158 PALETTEENTRY palette_entries[256];
6159 unsigned int i;
6160 static const struct
6162 DWORD caps, flags;
6163 BOOL attach_allowed;
6164 const char *name;
6166 test_data[] =
6168 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
6169 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
6170 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
6173 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6174 0, 0, 640, 480, 0, 0, 0, 0);
6175 ddraw = create_ddraw();
6176 ok(!!ddraw, "Failed to create a ddraw object.\n");
6177 if (FAILED(IDirectDraw_SetDisplayMode(ddraw, 640, 480, 8)))
6179 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
6180 IDirectDraw_Release(ddraw);
6181 DestroyWindow(window);
6182 return;
6184 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6185 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6187 memset(palette_entries, 0, sizeof(palette_entries));
6188 palette_entries[1].peFlags = 0x42;
6189 palette_entries[2].peFlags = 0xff;
6190 palette_entries[3].peFlags = 0x80;
6191 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
6192 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6194 memset(palette_entries, 0x66, sizeof(palette_entries));
6195 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
6196 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
6197 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
6198 palette_entries[0].peFlags);
6199 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
6200 palette_entries[1].peFlags);
6201 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
6202 palette_entries[2].peFlags);
6203 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
6204 palette_entries[3].peFlags);
6206 IDirectDrawPalette_Release(palette);
6208 memset(palette_entries, 0, sizeof(palette_entries));
6209 palette_entries[1].peFlags = 0x42;
6210 palette_entries[1].peRed = 0xff;
6211 palette_entries[2].peFlags = 0xff;
6212 palette_entries[3].peFlags = 0x80;
6213 hr = IDirectDraw_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
6214 palette_entries, &palette, NULL);
6215 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6217 memset(palette_entries, 0x66, sizeof(palette_entries));
6218 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
6219 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
6220 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
6221 palette_entries[0].peFlags);
6222 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
6223 palette_entries[1].peFlags);
6224 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
6225 palette_entries[2].peFlags);
6226 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
6227 palette_entries[3].peFlags);
6229 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
6231 memset(&surface_desc, 0, sizeof(surface_desc));
6232 surface_desc.dwSize = sizeof(surface_desc);
6233 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
6234 surface_desc.dwWidth = 128;
6235 surface_desc.dwHeight = 128;
6236 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
6237 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6238 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
6240 hr = IDirectDrawSurface_SetPalette(surface, palette);
6241 if (test_data[i].attach_allowed)
6242 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
6243 else
6244 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
6246 if (SUCCEEDED(hr))
6248 HDC dc;
6249 RGBQUAD rgbquad;
6250 UINT retval;
6252 hr = IDirectDrawSurface_GetDC(surface, &dc);
6253 ok(SUCCEEDED(hr) || broken(hr == DDERR_CANTCREATEDC) /* Win2k testbot */,
6254 "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
6255 if (SUCCEEDED(hr))
6257 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
6258 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
6259 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
6260 rgbquad.rgbRed, test_data[i].name);
6261 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
6262 rgbquad.rgbGreen, test_data[i].name);
6263 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
6264 rgbquad.rgbBlue, test_data[i].name);
6265 todo_wine ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
6266 rgbquad.rgbReserved, test_data[i].name);
6267 hr = IDirectDrawSurface_ReleaseDC(surface, dc);
6268 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6271 IDirectDrawSurface_Release(surface);
6274 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
6275 memset(&surface_desc, 0, sizeof(surface_desc));
6276 surface_desc.dwSize = sizeof(surface_desc);
6277 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6278 surface_desc.dwWidth = 128;
6279 surface_desc.dwHeight = 128;
6280 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6281 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
6282 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
6283 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
6284 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6285 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6286 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6287 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6288 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6289 hr = IDirectDrawSurface_SetPalette(surface, palette);
6290 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
6291 IDirectDrawSurface_Release(surface);
6293 /* The Windows 8 testbot keeps extra references to the primary
6294 * while in 8 bpp mode. */
6295 hr = IDirectDraw_RestoreDisplayMode(ddraw);
6296 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
6298 refcount = IDirectDrawPalette_Release(palette);
6299 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6300 refcount = IDirectDraw_Release(ddraw);
6301 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6302 DestroyWindow(window);
6305 static void test_lost_device(void)
6307 IDirectDrawSurface *surface;
6308 DDSURFACEDESC surface_desc;
6309 HWND window1, window2;
6310 IDirectDraw *ddraw;
6311 ULONG refcount;
6312 HRESULT hr;
6313 BOOL ret;
6315 window1 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6316 0, 0, 640, 480, 0, 0, 0, 0);
6317 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6318 0, 0, 640, 480, 0, 0, 0, 0);
6319 ddraw = create_ddraw();
6320 ok(!!ddraw, "Failed to create a ddraw object.\n");
6321 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6322 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6324 memset(&surface_desc, 0, sizeof(surface_desc));
6325 surface_desc.dwSize = sizeof(surface_desc);
6326 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
6327 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
6328 surface_desc.dwBackBufferCount = 1;
6329 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6330 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6332 hr = IDirectDrawSurface_IsLost(surface);
6333 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6334 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6335 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6337 ret = SetForegroundWindow(GetDesktopWindow());
6338 ok(ret, "Failed to set foreground window.\n");
6339 hr = IDirectDrawSurface_IsLost(surface);
6340 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6341 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6342 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6344 ret = SetForegroundWindow(window1);
6345 ok(ret, "Failed to set foreground window.\n");
6346 hr = IDirectDrawSurface_IsLost(surface);
6347 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6348 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6349 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6351 hr = restore_surfaces(ddraw);
6352 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6353 hr = IDirectDrawSurface_IsLost(surface);
6354 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6355 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6356 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6358 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
6359 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6360 hr = IDirectDrawSurface_IsLost(surface);
6361 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6362 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6363 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
6365 /* Trying to restore the primary will crash, probably because flippable
6366 * surfaces can't exist in DDSCL_NORMAL. */
6367 IDirectDrawSurface_Release(surface);
6368 memset(&surface_desc, 0, sizeof(surface_desc));
6369 surface_desc.dwSize = sizeof(surface_desc);
6370 surface_desc.dwFlags = DDSD_CAPS;
6371 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6372 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6373 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6375 hr = IDirectDrawSurface_IsLost(surface);
6376 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6378 ret = SetForegroundWindow(GetDesktopWindow());
6379 ok(ret, "Failed to set foreground window.\n");
6380 hr = IDirectDrawSurface_IsLost(surface);
6381 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6383 ret = SetForegroundWindow(window1);
6384 ok(ret, "Failed to set foreground window.\n");
6385 hr = IDirectDrawSurface_IsLost(surface);
6386 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6388 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6389 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6390 hr = IDirectDrawSurface_IsLost(surface);
6391 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6393 hr = restore_surfaces(ddraw);
6394 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6395 hr = IDirectDrawSurface_IsLost(surface);
6396 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6398 IDirectDrawSurface_Release(surface);
6399 memset(&surface_desc, 0, sizeof(surface_desc));
6400 surface_desc.dwSize = sizeof(surface_desc);
6401 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
6402 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
6403 U5(surface_desc).dwBackBufferCount = 1;
6404 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6405 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6407 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6408 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6409 hr = IDirectDrawSurface_IsLost(surface);
6410 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6411 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6412 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6414 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL | DDSCL_FULLSCREEN);
6415 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6416 hr = IDirectDrawSurface_IsLost(surface);
6417 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6418 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6419 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
6421 hr = IDirectDraw_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
6422 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6423 hr = IDirectDrawSurface_IsLost(surface);
6424 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6425 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6426 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
6428 hr = IDirectDraw_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
6429 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6430 hr = IDirectDrawSurface_IsLost(surface);
6431 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6432 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6433 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
6435 hr = IDirectDraw_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL | DDSCL_FULLSCREEN);
6436 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6437 hr = IDirectDrawSurface_IsLost(surface);
6438 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6439 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6440 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
6442 hr = IDirectDraw_SetCooperativeLevel(ddraw, window2, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6443 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
6444 hr = IDirectDrawSurface_IsLost(surface);
6445 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6446 hr = IDirectDrawSurface_Flip(surface, NULL, DDFLIP_WAIT);
6447 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
6449 IDirectDrawSurface_Release(surface);
6450 refcount = IDirectDraw_Release(ddraw);
6451 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6452 DestroyWindow(window2);
6453 DestroyWindow(window1);
6456 static void test_surface_desc_lock(void)
6458 IDirectDrawSurface *surface;
6459 DDSURFACEDESC surface_desc;
6460 IDirectDraw *ddraw;
6461 ULONG refcount;
6462 HWND window;
6463 HRESULT hr;
6465 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6466 0, 0, 640, 480, 0, 0, 0, 0);
6467 ddraw = create_ddraw();
6468 ok(!!ddraw, "Failed to create a ddraw object.\n");
6469 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6470 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6472 memset(&surface_desc, 0, sizeof(surface_desc));
6473 surface_desc.dwSize = sizeof(surface_desc);
6474 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6475 surface_desc.dwWidth = 16;
6476 surface_desc.dwHeight = 16;
6477 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6478 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6479 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6481 memset(&surface_desc, 0xaa, sizeof(surface_desc));
6482 surface_desc.dwSize = sizeof(surface_desc);
6483 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
6484 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6485 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
6487 memset(&surface_desc, 0xaa, sizeof(surface_desc));
6488 surface_desc.dwSize = sizeof(surface_desc);
6489 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, 0, NULL);
6490 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
6491 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
6492 memset(&surface_desc, 0xaa, sizeof(surface_desc));
6493 surface_desc.dwSize = sizeof(surface_desc);
6494 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
6495 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6496 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
6497 hr = IDirectDrawSurface_Unlock(surface, NULL);
6498 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
6500 memset(&surface_desc, 0xaa, sizeof(surface_desc));
6501 surface_desc.dwSize = sizeof(surface_desc);
6502 hr = IDirectDrawSurface_GetSurfaceDesc(surface, &surface_desc);
6503 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
6504 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
6506 IDirectDrawSurface_Release(surface);
6507 refcount = IDirectDraw_Release(ddraw);
6508 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6509 DestroyWindow(window);
6512 static void test_texturemapblend(void)
6514 HRESULT hr;
6515 DDSURFACEDESC ddsd;
6516 D3DEXECUTEBUFFERDESC exec_desc;
6517 DDBLTFX fx;
6518 static RECT rect = {0, 0, 64, 128};
6519 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
6520 DDCOLORKEY ckey;
6521 IDirectDrawSurface *surface, *rt;
6522 IDirect3DTexture *texture;
6523 D3DTEXTUREHANDLE texture_handle;
6524 HWND window;
6525 IDirectDraw *ddraw;
6526 IDirect3DDevice *device;
6527 IDirect3DMaterial *material;
6528 IDirect3DViewport *viewport;
6529 IDirect3DExecuteBuffer *execute_buffer;
6530 UINT inst_length;
6531 void *ptr;
6532 ULONG ref;
6533 D3DCOLOR color;
6535 static const D3DTLVERTEX test1_quads[] =
6537 {{0.0f}, {0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {0.0f}, {0.0f}},
6538 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {0.0f}, {1.0f}},
6539 {{640.0f}, {0.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {1.0f}, {0.0f}},
6540 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0xffffffff}, {0}, {1.0f}, {1.0f}},
6541 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {0.0f}, {0.0f}},
6542 {{0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {0.0f}, {1.0f}},
6543 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {1.0f}, {0.0f}},
6544 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x80ffffff}, {0}, {1.0f}, {1.0f}},
6546 test2_quads[] =
6548 {{0.0f}, {0.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {0.0f}, {0.0f}},
6549 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {0.0f}, {1.0f}},
6550 {{640.0f}, {0.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {1.0f}, {0.0f}},
6551 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x00ff0080}, {0}, {1.0f}, {1.0f}},
6552 {{0.0f}, {240.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {0.0f}, {0.0f}},
6553 {{0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {0.0f}, {1.0f}},
6554 {{640.0f}, {240.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {1.0f}, {0.0f}},
6555 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x008000ff}, {0}, {1.0f}, {1.0f}},
6558 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6559 0, 0, 640, 480, 0, 0, 0, 0);
6560 ddraw = create_ddraw();
6561 ok(!!ddraw, "Failed to create a ddraw object.\n");
6562 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
6564 skip("Failed to create a 3D device, skipping test.\n");
6565 DestroyWindow(window);
6566 IDirectDraw_Release(ddraw);
6567 return;
6570 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
6571 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6573 material = create_diffuse_material(device, 0.0f, 0.0f, 0.0f, 1.0f);
6574 viewport = create_viewport(device, 0, 0, 640, 480);
6575 viewport_set_background(device, viewport, material);
6577 memset(&exec_desc, 0, sizeof(exec_desc));
6578 exec_desc.dwSize = sizeof(exec_desc);
6579 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
6580 exec_desc.dwBufferSize = 1024;
6581 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
6582 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
6583 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
6585 /* Test alpha with DDPF_ALPHAPIXELS texture - should be taken from texture alpha channel.
6587 * The vertex alpha is completely ignored in this case, so case 1 and 2 combined are not
6588 * a D3DTOP_MODULATE with texture alpha = 0xff in case 2 (no alpha in texture). */
6589 memset(&ddsd, 0, sizeof(ddsd));
6590 ddsd.dwSize = sizeof(ddsd);
6591 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6592 ddsd.dwHeight = 128;
6593 ddsd.dwWidth = 128;
6594 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6595 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6596 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
6597 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
6598 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6599 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6600 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6601 U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
6602 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6603 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6605 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6606 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6607 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6608 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6610 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6611 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6613 memset(&fx, 0, sizeof(fx));
6614 fx.dwSize = sizeof(fx);
6615 U5(fx).dwFillColor = 0xff0000ff;
6616 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6617 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6618 U5(fx).dwFillColor = 0x800000ff;
6619 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6620 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6622 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6623 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6625 memcpy(exec_desc.lpData, test1_quads, sizeof(test1_quads));
6627 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test1_quads);
6628 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6629 emit_set_rs(&ptr, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
6630 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
6631 emit_set_rs(&ptr, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
6632 emit_set_rs(&ptr, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
6633 /* The history of D3DRENDERSTATE_ALPHABLENDENABLE is quite a mess. In the
6634 * first D3D release there was a D3DRENDERSTATE_BLENDENABLE (enum value 27).
6635 * D3D5 introduced a new and separate D3DRENDERSTATE_ALPHABLENDENABLE (42)
6636 * together with D3DRENDERSTATE_COLORKEYENABLE (41). The docs aren't all
6637 * that clear but they mention that D3DRENDERSTATE_BLENDENABLE overrides the
6638 * two new states.
6639 * Then D3D6 came and got rid of the new D3DRENDERSTATE_ALPHABLENDENABLE
6640 * state (42), renaming the older D3DRENDERSTATE_BLENDENABLE enum (27)
6641 * as D3DRENDERSTATE_ALPHABLENDENABLE.
6642 * There is a comment in the D3D6 docs which mentions that hardware
6643 * rasterizers always used D3DRENDERSTATE_BLENDENABLE to just toggle alpha
6644 * blending while prior to D3D5 software rasterizers toggled both color
6645 * keying and alpha blending according to it. What I gather is that, from
6646 * D3D6 onwards, D3DRENDERSTATE_ALPHABLENDENABLE always only toggles the
6647 * alpha blending state.
6648 * These tests seem to show that actual, current hardware follows the D3D6
6649 * behavior even when using the original D3D interfaces, for the HAL device
6650 * at least. */
6651 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
6652 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATE);
6653 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6655 emit_tquad(&ptr, 0);
6656 emit_tquad(&ptr, 4);
6657 emit_end(&ptr);
6659 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6660 inst_length -= sizeof(test1_quads);
6661 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6662 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6663 set_execute_data(execute_buffer, 8, sizeof(test1_quads), inst_length);
6665 hr = IDirect3DDevice_BeginScene(device);
6666 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6667 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6668 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6669 hr = IDirect3DDevice_EndScene(device);
6670 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6672 color = get_surface_color(rt, 5, 5);
6673 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6674 color = get_surface_color(rt, 400, 5);
6675 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6676 color = get_surface_color(rt, 5, 245);
6677 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6678 color = get_surface_color(rt, 400, 245);
6679 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6681 IDirect3DTexture_Release(texture);
6682 ref = IDirectDrawSurface_Release(surface);
6683 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6685 /* Test alpha with texture that has no alpha channel - alpha should be taken from diffuse vertex color. */
6686 memset(&ddsd, 0, sizeof(ddsd));
6687 ddsd.dwSize = sizeof(ddsd);
6688 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6689 ddsd.dwHeight = 128;
6690 ddsd.dwWidth = 128;
6691 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6692 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6693 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
6694 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
6695 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6696 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6697 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6699 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6700 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6702 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6703 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6704 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6705 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6707 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6708 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6710 U5(fx).dwFillColor = 0xff0000ff;
6711 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6712 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6713 U5(fx).dwFillColor = 0x800000ff;
6714 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6715 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6717 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6718 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6720 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test1_quads);
6721 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6722 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6724 emit_tquad(&ptr, 0);
6725 emit_tquad(&ptr, 4);
6726 emit_end(&ptr);
6728 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6729 inst_length -= sizeof(test1_quads);
6730 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6731 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6732 set_execute_data(execute_buffer, 8, sizeof(test1_quads), inst_length);
6734 hr = IDirect3DDevice_BeginScene(device);
6735 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6736 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6737 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6738 hr = IDirect3DDevice_EndScene(device);
6739 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6741 color = get_surface_color(rt, 5, 5);
6742 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6743 color = get_surface_color(rt, 400, 5);
6744 ok(compare_color(color, 0x000000ff, 2), "Got unexpected color 0x%08x.\n", color);
6745 color = get_surface_color(rt, 5, 245);
6746 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6747 color = get_surface_color(rt, 400, 245);
6748 ok(compare_color(color, 0x00000080, 2), "Got unexpected color 0x%08x.\n", color);
6750 IDirect3DTexture_Release(texture);
6751 ref = IDirectDrawSurface_Release(surface);
6752 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6754 /* Test RGB - should multiply color components from diffuse vertex color and texture. */
6755 memset(&ddsd, 0, sizeof(ddsd));
6756 ddsd.dwSize = sizeof(ddsd);
6757 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6758 ddsd.dwHeight = 128;
6759 ddsd.dwWidth = 128;
6760 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6761 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6762 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
6763 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 32;
6764 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6765 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6766 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000000ff;
6767 U5(ddsd.ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
6768 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6769 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6771 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6772 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6773 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6774 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6776 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6777 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6779 U5(fx).dwFillColor = 0x00ffffff;
6780 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6781 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6782 U5(fx).dwFillColor = 0x00ffff80;
6783 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6784 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6786 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6787 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6789 memcpy(exec_desc.lpData, test2_quads, sizeof(test2_quads));
6791 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test2_quads);
6792 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6793 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
6794 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6796 emit_tquad(&ptr, 0);
6797 emit_tquad(&ptr, 4);
6798 emit_end(&ptr);
6800 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6801 inst_length -= sizeof(test2_quads);
6802 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6803 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6804 set_execute_data(execute_buffer, 8, sizeof(test2_quads), inst_length);
6806 hr = IDirect3DDevice_BeginScene(device);
6807 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6808 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6809 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6810 hr = IDirect3DDevice_EndScene(device);
6811 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6813 /* WARP (Win8 testbot) emulates color keying with the alpha channel like Wine does,
6814 * but even applies it when there's no color key assigned. The surface alpha is zero
6815 * here, so nothing gets drawn.
6817 * The ddraw2 version of this test draws these quads with color keying off due to
6818 * different defaults in ddraw1 and ddraw2. */
6819 color = get_surface_color(rt, 5, 5);
6820 ok(compare_color(color, 0x00ff0040, 2) || broken(compare_color(color, 0x00000000, 1)),
6821 "Got unexpected color 0x%08x.\n", color);
6822 color = get_surface_color(rt, 400, 5);
6823 ok(compare_color(color, 0x00ff0080, 2) || broken(compare_color(color, 0x00000000, 1)),
6824 "Got unexpected color 0x%08x.\n", color);
6825 color = get_surface_color(rt, 5, 245);
6826 ok(compare_color(color, 0x00800080, 2) || broken(compare_color(color, 0x00000000, 1)),
6827 "Got unexpected color 0x%08x.\n", color);
6828 color = get_surface_color(rt, 400, 245);
6829 ok(compare_color(color, 0x008000ff, 2) || broken(compare_color(color, 0x00000000, 1)),
6830 "Got unexpected color 0x%08x.\n", color);
6832 IDirect3DTexture_Release(texture);
6833 ref = IDirectDrawSurface_Release(surface);
6834 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6836 /* Test alpha again, now with color keyed texture (colorkey emulation in wine can interfere). */
6837 memset(&ddsd, 0, sizeof(ddsd));
6838 ddsd.dwSize = sizeof(ddsd);
6839 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
6840 ddsd.dwHeight = 128;
6841 ddsd.dwWidth = 128;
6842 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6843 ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
6844 ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
6845 U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 16;
6846 U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xf800;
6847 U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x07e0;
6848 U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x001f;
6850 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
6851 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6853 hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DTexture, (void **)&texture);
6854 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
6855 hr = IDirect3DTexture_GetHandle(texture, device, &texture_handle);
6856 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
6858 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6859 ok(SUCCEEDED(hr), "Failed to clear render target, hr %#x.\n", hr);
6861 U5(fx).dwFillColor = 0xf800;
6862 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6863 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6864 U5(fx).dwFillColor = 0x001f;
6865 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6866 ok(SUCCEEDED(hr), "Failed to clear texture, hr %#x.\n", hr);
6868 ckey.dwColorSpaceLowValue = 0x001f;
6869 ckey.dwColorSpaceHighValue = 0x001f;
6870 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
6871 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
6873 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
6874 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
6876 memcpy(exec_desc.lpData, test1_quads, sizeof(test1_quads));
6878 ptr = ((BYTE *)exec_desc.lpData) + sizeof(test1_quads);
6879 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
6880 emit_set_rs(&ptr, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
6881 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, texture_handle);
6882 /* D3DRENDERSTATE_COLORKEYENABLE is supposed to be on by default on version
6883 * 1 devices, but for some reason it randomly defaults to FALSE on the W8
6884 * testbot. This is either the fault of Windows 8 or the WARP driver.
6885 * Also D3DRENDERSTATE_COLORKEYENABLE was introduced in D3D 5 aka version 2
6886 * devices only, which might imply this doesn't actually do anything on
6887 * WARP. */
6888 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
6890 emit_tquad(&ptr, 0);
6891 emit_tquad(&ptr, 4);
6892 emit_end(&ptr);
6894 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
6895 inst_length -= sizeof(test1_quads);
6896 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
6897 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
6898 set_execute_data(execute_buffer, 8, sizeof(test1_quads), inst_length);
6900 hr = IDirect3DDevice_BeginScene(device);
6901 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
6902 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
6903 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
6904 hr = IDirect3DDevice_EndScene(device);
6905 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
6907 /* Allow broken WARP results (colorkey disabled). */
6908 color = get_surface_color(rt, 5, 5);
6909 ok(compare_color(color, 0x00000000, 2) || broken(compare_color(color, 0x000000ff, 2)),
6910 "Got unexpected color 0x%08x.\n", color);
6911 color = get_surface_color(rt, 400, 5);
6912 ok(compare_color(color, 0x00ff0000, 2), "Got unexpected color 0x%08x.\n", color);
6913 color = get_surface_color(rt, 5, 245);
6914 ok(compare_color(color, 0x00000000, 2) || broken(compare_color(color, 0x00000080, 2)),
6915 "Got unexpected color 0x%08x.\n", color);
6916 color = get_surface_color(rt, 400, 245);
6917 ok(compare_color(color, 0x00800000, 2), "Got unexpected color 0x%08x.\n", color);
6919 IDirect3DTexture_Release(texture);
6920 ref = IDirectDrawSurface_Release(surface);
6921 ok(ref == 0, "Surface not properly released, refcount %u.\n", ref);
6923 ref = IDirect3DExecuteBuffer_Release(execute_buffer);
6924 ok(ref == 0, "Execute buffer not properly released, refcount %u.\n", ref);
6925 destroy_viewport(device, viewport);
6926 ref = IDirect3DMaterial_Release(material);
6927 ok(ref == 0, "Material not properly released, refcount %u.\n", ref);
6928 IDirectDrawSurface_Release(rt);
6929 IDirect3DDevice_Release(device);
6930 ref = IDirectDraw_Release(ddraw);
6931 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6932 DestroyWindow(window);
6935 static void test_viewport_clear_rect(void)
6937 HRESULT hr;
6938 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
6939 static D3DRECT clear_rect2 = {{90}, {90}, {110}, {110}};
6940 IDirectDrawSurface *rt;
6941 HWND window;
6942 IDirectDraw *ddraw;
6943 IDirect3DDevice *device;
6944 IDirect3DMaterial *red, *green;
6945 IDirect3DViewport *viewport, *viewport2;
6946 ULONG ref;
6947 D3DCOLOR color;
6949 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6950 0, 0, 640, 480, 0, 0, 0, 0);
6951 ddraw = create_ddraw();
6952 ok(!!ddraw, "Failed to create a ddraw object.\n");
6953 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
6955 skip("Failed to create a 3D device, skipping test.\n");
6956 DestroyWindow(window);
6957 IDirectDraw_Release(ddraw);
6958 return;
6961 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
6962 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
6964 red = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
6965 viewport = create_viewport(device, 0, 0, 640, 480);
6966 viewport_set_background(device, viewport, red);
6967 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
6968 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6970 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 1.0f);
6971 viewport2 = create_viewport(device, 100, 100, 20, 20);
6972 viewport_set_background(device, viewport2, green);
6973 hr = IDirect3DViewport_Clear(viewport2, 1, &clear_rect2, D3DCLEAR_TARGET);
6974 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
6976 color = get_surface_color(rt, 85, 85); /* Outside both. */
6977 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6978 color = get_surface_color(rt, 95, 95); /* Outside vp, inside rect. */
6979 /* AMD GPUs ignore the viewport dimensions and only care about the rectangle. */
6980 ok(compare_color(color, 0x00ff0000, 1) || broken(compare_color(color, 0x0000ff00, 1)),
6981 "Got unexpected color 0x%08x.\n", color);
6982 color = get_surface_color(rt, 105, 105); /* Inside both. */
6983 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
6984 color = get_surface_color(rt, 115, 115); /* Inside vp, outside rect. */
6985 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6986 color = get_surface_color(rt, 125, 125); /* Outside both. */
6987 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
6989 destroy_viewport(device, viewport2);
6990 destroy_material(green);
6991 destroy_viewport(device, viewport);
6992 destroy_material(red);
6993 IDirectDrawSurface_Release(rt);
6994 IDirect3DDevice_Release(device);
6995 ref = IDirectDraw_Release(ddraw);
6996 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6997 DestroyWindow(window);
7000 static void test_color_fill(void)
7002 HRESULT hr;
7003 IDirect3DDevice *device;
7004 IDirectDraw *ddraw;
7005 IDirectDrawSurface *surface, *surface2;
7006 DDSURFACEDESC surface_desc;
7007 ULONG refcount;
7008 HWND window;
7009 unsigned int i;
7010 DDBLTFX fx;
7011 RECT rect = {5, 5, 7, 7};
7012 DWORD *color;
7013 DWORD num_fourcc_codes, *fourcc_codes;
7014 DDCAPS hal_caps;
7015 BOOL support_uyvy = FALSE, support_yuy2 = FALSE;
7016 static const struct
7018 DWORD caps;
7019 HRESULT colorfill_hr, depthfill_hr;
7020 BOOL rop_success;
7021 const char *name;
7022 DWORD result;
7023 BOOL check_result;
7024 DDPIXELFORMAT format;
7026 tests[] =
7029 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
7030 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain RGB", 0xdeadbeef, TRUE,
7032 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
7033 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
7037 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
7038 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain RGB", 0xdeadbeef, TRUE,
7040 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
7041 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
7045 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
7046 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem texture RGB", 0xdeadbeef, TRUE,
7048 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
7049 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
7053 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
7054 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem texture RGB", 0xdeadbeef, TRUE,
7056 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
7057 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
7061 DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY,
7062 DDERR_INVALIDPARAMS, DD_OK, TRUE, "vidmem zbuffer", 0, FALSE,
7063 {0, 0, 0, {0}, {0}, {0}, {0}, {0}}
7066 /* Colorfill on YUV surfaces always returns DD_OK, but the content is
7067 * different afterwards. DX9+ GPUs set one of the two luminance values
7068 * in each block, but AMD and Nvidia GPUs disagree on which luminance
7069 * value they set. r200 (dx8) just sets the entire block to the clear
7070 * value. */
7071 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
7072 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain YUY2", 0, FALSE,
7074 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
7075 {0}, {0}, {0}, {0}, {0}
7079 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
7080 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem offscreenplain UYVY", 0, FALSE,
7082 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
7083 {0}, {0}, {0}, {0}, {0}
7087 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY,
7088 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay YUY2", 0, FALSE,
7090 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y', 'U', 'Y', '2'),
7091 {0}, {0}, {0}, {0}, {0}
7095 DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY,
7096 DD_OK, DDERR_INVALIDPARAMS, FALSE, "vidmem overlay UYVY", 0, FALSE,
7098 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U', 'Y', 'V', 'Y'),
7099 {0}, {0}, {0}, {0}, {0}
7103 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
7104 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "vidmem texture DXT1", 0, FALSE,
7106 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
7107 {0}, {0}, {0}, {0}, {0}
7111 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
7112 E_NOTIMPL, DDERR_INVALIDPARAMS, FALSE, "sysmem texture DXT1", 0, FALSE,
7114 sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('D', 'X', 'T', '1'),
7115 {0}, {0}, {0}, {0}, {0}
7119 /* The testbot fills this with 0x00 instead of the blue channel. The sysmem
7120 * surface works, presumably because it is handled by the runtime instead of
7121 * the driver. */
7122 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
7123 DD_OK, DDERR_INVALIDPARAMS, TRUE, "vidmem offscreenplain P8", 0xefefefef, FALSE,
7125 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
7126 {8}, {0}, {0}, {0}, {0}
7130 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
7131 DD_OK, DDERR_INVALIDPARAMS, TRUE, "sysmem offscreenplain P8", 0xefefefef, TRUE,
7133 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_PALETTEINDEXED8, 0,
7134 {8}, {0}, {0}, {0}, {0}
7138 static const struct
7140 DWORD rop;
7141 const char *name;
7142 HRESULT hr;
7144 rops[] =
7146 {SRCCOPY, "SRCCOPY", DD_OK},
7147 {SRCPAINT, "SRCPAINT", DDERR_NORASTEROPHW},
7148 {SRCAND, "SRCAND", DDERR_NORASTEROPHW},
7149 {SRCINVERT, "SRCINVERT", DDERR_NORASTEROPHW},
7150 {SRCERASE, "SRCERASE", DDERR_NORASTEROPHW},
7151 {NOTSRCCOPY, "NOTSRCCOPY", DDERR_NORASTEROPHW},
7152 {NOTSRCERASE, "NOTSRCERASE", DDERR_NORASTEROPHW},
7153 {MERGECOPY, "MERGECOPY", DDERR_NORASTEROPHW},
7154 {MERGEPAINT, "MERGEPAINT", DDERR_NORASTEROPHW},
7155 {PATCOPY, "PATCOPY", DDERR_NORASTEROPHW},
7156 {PATPAINT, "PATPAINT", DDERR_NORASTEROPHW},
7157 {PATINVERT, "PATINVERT", DDERR_NORASTEROPHW},
7158 {DSTINVERT, "DSTINVERT", DDERR_NORASTEROPHW},
7159 {BLACKNESS, "BLACKNESS", DD_OK},
7160 {WHITENESS, "WHITENESS", DD_OK},
7161 {0xaa0029, "0xaa0029", DDERR_NORASTEROPHW} /* noop */
7164 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7165 0, 0, 640, 480, 0, 0, 0, 0);
7166 ddraw = create_ddraw();
7167 ok(!!ddraw, "Failed to create a ddraw object.\n");
7168 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
7170 skip("Failed to create a 3D device, skipping test.\n");
7171 DestroyWindow(window);
7172 IDirectDraw_Release(ddraw);
7173 return;
7176 hr = IDirectDraw_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
7177 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
7178 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
7179 num_fourcc_codes * sizeof(*fourcc_codes));
7180 if (!fourcc_codes)
7181 goto done;
7182 hr = IDirectDraw_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
7183 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
7184 for (i = 0; i < num_fourcc_codes; i++)
7186 if (fourcc_codes[i] == MAKEFOURCC('Y', 'U', 'Y', '2'))
7187 support_yuy2 = TRUE;
7188 else if (fourcc_codes[i] == MAKEFOURCC('U', 'Y', 'V', 'Y'))
7189 support_uyvy = TRUE;
7191 HeapFree(GetProcessHeap(), 0, fourcc_codes);
7193 memset(&hal_caps, 0, sizeof(hal_caps));
7194 hal_caps.dwSize = sizeof(hal_caps);
7195 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
7196 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7198 if ((!support_yuy2 && !support_uyvy) || !(hal_caps.dwCaps & DDCAPS_OVERLAY))
7199 skip("Overlays or some YUV formats not supported, skipping YUV colorfill tests.\n");
7201 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
7203 /* Some Windows drivers modify dwFillColor when it is used on P8 or FourCC formats. */
7204 memset(&fx, 0, sizeof(fx));
7205 fx.dwSize = sizeof(fx);
7206 U5(fx).dwFillColor = 0xdeadbeef;
7208 memset(&surface_desc, 0, sizeof(surface_desc));
7209 surface_desc.dwSize = sizeof(surface_desc);
7210 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7211 surface_desc.dwWidth = 64;
7212 surface_desc.dwHeight = 64;
7213 surface_desc.ddpfPixelFormat = tests[i].format;
7214 surface_desc.ddsCaps.dwCaps = tests[i].caps;
7216 if (tests[i].caps & DDSCAPS_TEXTURE)
7218 struct format_support_check check = {&tests[i].format, FALSE};
7219 hr = IDirect3DDevice_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
7220 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
7221 if (!check.supported)
7222 continue;
7225 if (tests[i].format.dwFourCC == MAKEFOURCC('Y','U','Y','2') && !support_yuy2)
7226 continue;
7227 if (tests[i].format.dwFourCC == MAKEFOURCC('U','Y','V','Y') && !support_uyvy)
7228 continue;
7229 if (tests[i].caps & DDSCAPS_OVERLAY && !(hal_caps.dwCaps & DDCAPS_OVERLAY))
7230 continue;
7232 if (tests[i].caps & DDSCAPS_ZBUFFER)
7234 surface_desc.dwFlags &= ~DDSD_PIXELFORMAT;
7235 surface_desc.dwFlags |= DDSD_ZBUFFERBITDEPTH;
7236 U2(surface_desc).dwZBufferBitDepth = get_device_z_depth(device);
7239 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7240 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, surface %s.\n", hr, tests[i].name);
7242 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7243 if (tests[i].format.dwFourCC)
7244 todo_wine ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
7245 hr, tests[i].colorfill_hr, tests[i].name);
7246 else
7247 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
7248 hr, tests[i].colorfill_hr, tests[i].name);
7250 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7251 if (tests[i].format.dwFourCC)
7252 todo_wine ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
7253 hr, tests[i].colorfill_hr, tests[i].name);
7254 else
7255 ok(hr == tests[i].colorfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
7256 hr, tests[i].colorfill_hr, tests[i].name);
7258 if (SUCCEEDED(hr) && tests[i].check_result)
7260 memset(&surface_desc, 0, sizeof(surface_desc));
7261 surface_desc.dwSize = sizeof(surface_desc);
7262 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
7263 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
7264 color = surface_desc.lpSurface;
7265 ok(*color == tests[i].result, "Got clear result 0x%08x, expected 0x%08x, surface %s.\n",
7266 *color, tests[i].result, tests[i].name);
7267 hr = IDirectDrawSurface_Unlock(surface, NULL);
7268 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
7271 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7272 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
7273 hr, tests[i].depthfill_hr, tests[i].name);
7274 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7275 ok(hr == tests[i].depthfill_hr, "Blt returned %#x, expected %#x, surface %s.\n",
7276 hr, tests[i].depthfill_hr, tests[i].name);
7278 U5(fx).dwFillColor = 0xdeadbeef;
7279 fx.dwROP = BLACKNESS;
7280 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
7281 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
7282 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
7283 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
7284 U5(fx).dwFillColor, tests[i].name);
7286 if (SUCCEEDED(hr) && tests[i].check_result)
7288 memset(&surface_desc, 0, sizeof(surface_desc));
7289 surface_desc.dwSize = sizeof(surface_desc);
7290 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
7291 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
7292 color = surface_desc.lpSurface;
7293 ok(*color == 0, "Got clear result 0x%08x, expected 0x00000000, surface %s.\n",
7294 *color, tests[i].name);
7295 hr = IDirectDrawSurface_Unlock(surface, NULL);
7296 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
7299 fx.dwROP = WHITENESS;
7300 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
7301 ok(FAILED(hr) == !tests[i].rop_success, "Blt returned %#x, expected %s, surface %s.\n",
7302 hr, tests[i].rop_success ? "success" : "failure", tests[i].name);
7303 ok(U5(fx).dwFillColor == 0xdeadbeef, "dwFillColor was set to 0x%08x, surface %s\n",
7304 U5(fx).dwFillColor, tests[i].name);
7306 if (SUCCEEDED(hr) && tests[i].check_result)
7308 memset(&surface_desc, 0, sizeof(surface_desc));
7309 surface_desc.dwSize = sizeof(surface_desc);
7310 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_READONLY, 0);
7311 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, surface %s.\n", hr, tests[i].name);
7312 color = surface_desc.lpSurface;
7313 /* WHITENESS sets the alpha channel to 0x00. Ignore this for now. */
7314 ok((*color & 0x00ffffff) == 0x00ffffff, "Got clear result 0x%08x, expected 0xffffffff, surface %s.\n",
7315 *color, tests[i].name);
7316 hr = IDirectDrawSurface_Unlock(surface, NULL);
7317 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, surface %s.\n", hr, tests[i].name);
7320 IDirectDrawSurface_Release(surface);
7323 memset(&fx, 0, sizeof(fx));
7324 fx.dwSize = sizeof(fx);
7325 U5(fx).dwFillColor = 0xdeadbeef;
7326 fx.dwROP = WHITENESS;
7328 memset(&surface_desc, 0, sizeof(surface_desc));
7329 surface_desc.dwSize = sizeof(surface_desc);
7330 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7331 surface_desc.dwWidth = 64;
7332 surface_desc.dwHeight = 64;
7333 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
7334 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
7335 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
7336 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7337 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7338 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
7339 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
7340 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7341 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7342 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
7343 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7345 /* No DDBLTFX. */
7346 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, NULL);
7347 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7348 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, NULL);
7349 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7351 /* Unused source rectangle. */
7352 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7353 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7354 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
7355 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7357 /* Unused source surface. */
7358 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7359 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7360 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
7361 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7362 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7363 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7364 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
7365 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7367 /* Inverted destination or source rectangle. */
7368 SetRect(&rect, 5, 7, 7, 5);
7369 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7370 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7371 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7372 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7373 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7374 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7375 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7376 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7377 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
7378 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7380 /* Negative rectangle. */
7381 SetRect(&rect, -1, -1, 5, 5);
7382 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7383 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7384 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7385 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7386 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7387 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7388 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, &rect, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7389 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7390 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
7391 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7393 /* Out of bounds rectangle. */
7394 SetRect(&rect, 0, 0, 65, 65);
7395 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7396 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7397 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_ROP | DDBLT_WAIT, &fx);
7398 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7400 /* Combine multiple flags. */
7401 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7402 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7403 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
7404 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7405 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_ROP | DDBLT_WAIT, &fx);
7406 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7408 for (i = 0; i < sizeof(rops) / sizeof(*rops); i++)
7410 fx.dwROP = rops[i].rop;
7411 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_ROP | DDBLT_WAIT, &fx);
7412 ok(hr == rops[i].hr, "Got unexpected hr %#x for rop %s.\n", hr, rops[i].name);
7415 IDirectDrawSurface_Release(surface2);
7416 IDirectDrawSurface_Release(surface);
7418 memset(&surface_desc, 0, sizeof(surface_desc));
7419 surface_desc.dwSize = sizeof(surface_desc);
7420 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_ZBUFFERBITDEPTH;
7421 surface_desc.dwWidth = 64;
7422 surface_desc.dwHeight = 64;
7423 U2(surface_desc).dwZBufferBitDepth = get_device_z_depth(device);
7424 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
7425 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7426 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7427 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
7428 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7430 /* No DDBLTFX. */
7431 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, NULL);
7432 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7434 /* Unused source rectangle. */
7435 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7436 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7438 /* Unused source surface. */
7439 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7440 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7441 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7442 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7444 /* Inverted destination or source rectangle. */
7445 SetRect(&rect, 5, 7, 7, 5);
7446 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7447 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7448 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7449 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7450 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7451 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7452 hr = IDirectDrawSurface_Blt(surface, NULL, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7453 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7455 /* Negative rectangle. */
7456 SetRect(&rect, -1, -1, 5, 5);
7457 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7458 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7459 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7460 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
7461 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7462 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7463 hr = IDirectDrawSurface_Blt(surface, &rect, surface2, &rect, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7464 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7466 /* Out of bounds rectangle. */
7467 SetRect(&rect, 0, 0, 65, 65);
7468 hr = IDirectDrawSurface_Blt(surface, &rect, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7469 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
7471 /* Combine multiple flags. */
7472 hr = IDirectDrawSurface_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
7473 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7475 IDirectDrawSurface_Release(surface2);
7476 IDirectDrawSurface_Release(surface);
7478 done:
7479 IDirect3DDevice_Release(device);
7480 refcount = IDirectDraw_Release(ddraw);
7481 ok(refcount == 0, "Ddraw object not properly released, refcount %u.\n", refcount);
7482 DestroyWindow(window);
7485 static void test_colorkey_precision(void)
7487 static D3DTLVERTEX quad[] =
7489 {{ 0.0f}, {480.0f}, {0.0f}, {1.0f}, {0x00000000}, {0x00000000}, {0.0f}, {1.0f}},
7490 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0x00000000}, {0x00000000}, {0.0f}, {0.0f}},
7491 {{640.0f}, {480.0f}, {0.0f}, {1.0f}, {0x00000000}, {0x00000000}, {1.0f}, {1.0f}},
7492 {{640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0x00000000}, {0x00000000}, {1.0f}, {0.0f}},
7494 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
7495 IDirect3DDevice *device;
7496 IDirectDraw *ddraw;
7497 IDirectDrawSurface *rt;
7498 IDirect3DViewport *viewport;
7499 IDirect3DExecuteBuffer *execute_buffer;
7500 D3DEXECUTEBUFFERDESC exec_desc;
7501 UINT inst_length;
7502 void *ptr;
7503 HWND window;
7504 HRESULT hr;
7505 IDirectDrawSurface *src, *dst, *texture;
7506 D3DTEXTUREHANDLE handle;
7507 IDirect3DTexture *d3d_texture;
7508 IDirect3DMaterial *green;
7509 DDSURFACEDESC surface_desc, lock_desc;
7510 ULONG refcount;
7511 D3DCOLOR color;
7512 unsigned int t, c;
7513 DDCOLORKEY ckey;
7514 DDBLTFX fx;
7515 DWORD data[4] = {0}, color_mask;
7516 D3DDEVICEDESC device_desc, hel_desc;
7517 BOOL warp;
7518 static const struct
7520 unsigned int max, shift, bpp, clear;
7521 const char *name;
7522 DDPIXELFORMAT fmt;
7524 tests[] =
7527 255, 0, 4, 0x00345678, "D3DFMT_X8R8G8B8",
7529 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
7530 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0x00000000}
7535 63, 5, 2, 0x5678, "D3DFMT_R5G6B5, G channel",
7537 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
7538 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
7543 31, 0, 2, 0x5678, "D3DFMT_R5G6B5, B channel",
7545 sizeof(DDPIXELFORMAT), DDPF_RGB, 0,
7546 {16}, {0xf800}, {0x07e0}, {0x001f}, {0x0000}
7551 15, 0, 2, 0x0678, "D3DFMT_A4R4G4B4",
7553 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
7554 {16}, {0x0f00}, {0x00f0}, {0x000f}, {0xf000}
7560 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7561 0, 0, 640, 480, 0, 0, 0, 0);
7562 ddraw = create_ddraw();
7563 ok(!!ddraw, "Failed to create a ddraw object.\n");
7564 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
7566 skip("Failed to create a 3D device, skipping test.\n");
7567 DestroyWindow(window);
7568 IDirectDraw_Release(ddraw);
7569 return;
7571 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
7572 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
7574 /* The Windows 8 WARP driver has plenty of false negatives in X8R8G8B8
7575 * (color key doesn't match although the values are equal), and a false
7576 * positive when the color key is 0 and the texture contains the value 1.
7577 * I don't want to mark this broken unconditionally since this would
7578 * essentially disable the test on Windows. Try to detect WARP (and I
7579 * guess mismatch other SW renderers) by its ability to texture from
7580 * system memory. Also on random occasions 254 == 255 and 255 != 255.*/
7581 memset(&device_desc, 0, sizeof(device_desc));
7582 device_desc.dwSize = sizeof(device_desc);
7583 memset(&hel_desc, 0, sizeof(hel_desc));
7584 hel_desc.dwSize = sizeof(hel_desc);
7585 hr = IDirect3DDevice_GetCaps(device, &device_desc, &hel_desc);
7586 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
7587 warp = !!(device_desc.dwDevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY);
7589 green = create_diffuse_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
7590 viewport = create_viewport(device, 0, 0, 640, 480);
7591 viewport_set_background(device, viewport, green);
7593 memset(&exec_desc, 0, sizeof(exec_desc));
7594 exec_desc.dwSize = sizeof(exec_desc);
7595 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
7596 exec_desc.dwBufferSize = 1024;
7597 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
7598 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
7599 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
7601 memset(&fx, 0, sizeof(fx));
7602 fx.dwSize = sizeof(fx);
7603 memset(&lock_desc, 0, sizeof(lock_desc));
7604 lock_desc.dwSize = sizeof(lock_desc);
7606 for (t = 0; t < sizeof(tests) / sizeof(*tests); ++t)
7608 memset(&surface_desc, 0, sizeof(surface_desc));
7609 surface_desc.dwSize = sizeof(surface_desc);
7610 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7611 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7612 surface_desc.dwWidth = 4;
7613 surface_desc.dwHeight = 1;
7614 surface_desc.ddpfPixelFormat = tests[t].fmt;
7615 /* Windows XP (at least with the r200 driver, other drivers untested) produces
7616 * garbage when doing color keyed texture->texture blits. */
7617 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &src, NULL);
7618 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7619 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &dst, NULL);
7620 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7622 fx.dwFillColor = tests[t].clear;
7623 /* On the w8 testbot (WARP driver) the blit result has different values in the
7624 * X channel. */
7625 color_mask = U2(tests[t].fmt).dwRBitMask
7626 | U3(tests[t].fmt).dwGBitMask
7627 | U4(tests[t].fmt).dwBBitMask;
7629 for (c = 0; c <= tests[t].max; ++c)
7631 /* The idiotic Nvidia Windows driver can't change the color key on a d3d
7632 * texture after it has been set once... */
7633 surface_desc.dwFlags |= DDSD_CKSRCBLT;
7634 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
7635 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = c << tests[t].shift;
7636 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = c << tests[t].shift;
7637 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &texture, NULL);
7638 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7640 hr = IDirectDrawSurface_QueryInterface(texture, &IID_IDirect3DTexture, (void **)&d3d_texture);
7641 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
7642 hr = IDirect3DTexture_GetHandle(d3d_texture, device, &handle);
7643 ok(SUCCEEDED(hr), "Failed to get texture handle, hr %#x.\n", hr);
7644 IDirect3DTexture_Release(d3d_texture);
7646 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
7647 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
7649 memcpy(exec_desc.lpData, quad, sizeof(quad));
7651 ptr = ((BYTE *)exec_desc.lpData) + sizeof(quad);
7652 emit_process_vertices(&ptr, D3DPROCESSVERTICES_COPY, 0, 8);
7653 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
7654 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, handle);
7655 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATEALPHA);
7656 /* D3DRENDERSTATE_COLORKEYENABLE is supposed to be on by default on version
7657 * 1 devices, but for some reason it randomly defaults to FALSE on the W8
7658 * testbot. This is either the fault of Windows 8 or the WARP driver.
7659 * Also D3DRENDERSTATE_COLORKEYENABLE was introduced in D3D 5 aka version 2
7660 * devices only, which might imply this doesn't actually do anything on
7661 * WARP. */
7662 emit_set_rs(&ptr, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
7664 emit_tquad(&ptr, 0);
7665 emit_tquad(&ptr, 4);
7666 emit_set_rs(&ptr, D3DRENDERSTATE_TEXTUREHANDLE, 0);
7667 emit_end(&ptr);
7669 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
7670 inst_length -= sizeof(quad);
7671 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
7672 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
7673 set_execute_data(execute_buffer, 8, sizeof(quad), inst_length);
7675 hr = IDirectDrawSurface_Blt(dst, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
7676 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
7678 hr = IDirectDrawSurface_Lock(src, NULL, &lock_desc, DDLOCK_WAIT, NULL);
7679 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7680 switch (tests[t].bpp)
7682 case 4:
7683 ((DWORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
7684 ((DWORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
7685 ((DWORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
7686 ((DWORD *)lock_desc.lpSurface)[3] = 0xffffffff;
7687 break;
7689 case 2:
7690 ((WORD *)lock_desc.lpSurface)[0] = (c ? c - 1 : 0) << tests[t].shift;
7691 ((WORD *)lock_desc.lpSurface)[1] = c << tests[t].shift;
7692 ((WORD *)lock_desc.lpSurface)[2] = min(c + 1, tests[t].max) << tests[t].shift;
7693 ((WORD *)lock_desc.lpSurface)[3] = 0xffff;
7694 break;
7696 hr = IDirectDrawSurface_Unlock(src, 0);
7697 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
7698 hr = IDirectDrawSurface_Blt(texture, NULL, src, NULL, DDBLT_WAIT, NULL);
7699 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
7701 ckey.dwColorSpaceLowValue = c << tests[t].shift;
7702 ckey.dwColorSpaceHighValue = c << tests[t].shift;
7703 hr = IDirectDrawSurface_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
7704 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
7706 hr = IDirectDrawSurface_Blt(dst, NULL, src, NULL, DDBLT_KEYSRC | DDBLT_WAIT, NULL);
7707 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
7709 /* Don't make this read only, it somehow breaks the detection of the Nvidia bug below. */
7710 hr = IDirectDrawSurface_Lock(dst, NULL, &lock_desc, DDLOCK_WAIT, NULL);
7711 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7712 switch (tests[t].bpp)
7714 case 4:
7715 data[0] = ((DWORD *)lock_desc.lpSurface)[0] & color_mask;
7716 data[1] = ((DWORD *)lock_desc.lpSurface)[1] & color_mask;
7717 data[2] = ((DWORD *)lock_desc.lpSurface)[2] & color_mask;
7718 data[3] = ((DWORD *)lock_desc.lpSurface)[3] & color_mask;
7719 break;
7721 case 2:
7722 data[0] = ((WORD *)lock_desc.lpSurface)[0] & color_mask;
7723 data[1] = ((WORD *)lock_desc.lpSurface)[1] & color_mask;
7724 data[2] = ((WORD *)lock_desc.lpSurface)[2] & color_mask;
7725 data[3] = ((WORD *)lock_desc.lpSurface)[3] & color_mask;
7726 break;
7728 hr = IDirectDrawSurface_Unlock(dst, 0);
7729 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
7731 if (!c)
7733 ok(data[0] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
7734 tests[t].clear, data[0], tests[t].name, c);
7736 if (data[3] == tests[t].clear)
7738 /* My Geforce GTX 460 on Windows 7 misbehaves when A4R4G4B4 is blitted with color
7739 * keying: The blit takes ~0.5 seconds, and subsequent color keying draws are broken,
7740 * even when a different surface is used. The blit itself doesn't draw anything,
7741 * so we can detect the bug by looking at the otherwise unused 4th texel. It should
7742 * never be masked out by the key.
7744 * Also appears to affect the testbot in some way with R5G6B5. Color keying is
7745 * terrible on WARP. */
7746 skip("Nvidia A4R4G4B4 color keying blit bug detected, skipping.\n");
7747 IDirectDrawSurface_Release(texture);
7748 IDirectDrawSurface_Release(src);
7749 IDirectDrawSurface_Release(dst);
7750 goto done;
7753 else
7754 ok(data[0] == (c - 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
7755 (c - 1) << tests[t].shift, data[0], tests[t].name, c);
7757 ok(data[1] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
7758 tests[t].clear, data[1], tests[t].name, c);
7760 if (c == tests[t].max)
7761 ok(data[2] == tests[t].clear, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
7762 tests[t].clear, data[2], tests[t].name, c);
7763 else
7764 ok(data[2] == (c + 1) << tests[t].shift, "Expected surface content %#x, got %#x, format %s, c=%u.\n",
7765 (c + 1) << tests[t].shift, data[2], tests[t].name, c);
7767 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7768 ok(SUCCEEDED(hr), "Failed to clear, hr %#x.\n", hr);
7770 hr = IDirect3DDevice_BeginScene(device);
7771 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7772 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_UNCLIPPED);
7773 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7774 hr = IDirect3DDevice_EndScene(device);
7775 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7777 color = get_surface_color(rt, 80, 240);
7778 if (!c)
7779 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
7780 "Got unexpected color 0x%08x, format %s, c=%u.\n",
7781 color, tests[t].name, c);
7782 else
7783 ok(compare_color(color, 0x00000000, 1) || broken(warp && compare_color(color, 0x0000ff00, 1)),
7784 "Got unexpected color 0x%08x, format %s, c=%u.\n",
7785 color, tests[t].name, c);
7787 color = get_surface_color(rt, 240, 240);
7788 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
7789 "Got unexpected color 0x%08x, format %s, c=%u.\n",
7790 color, tests[t].name, c);
7792 color = get_surface_color(rt, 400, 240);
7793 if (c == tests[t].max)
7794 ok(compare_color(color, 0x0000ff00, 1) || broken(warp && compare_color(color, 0x00000000, 1)),
7795 "Got unexpected color 0x%08x, format %s, c=%u.\n",
7796 color, tests[t].name, c);
7797 else
7798 ok(compare_color(color, 0x00000000, 1) || broken(warp && compare_color(color, 0x0000ff00, 1)),
7799 "Got unexpected color 0x%08x, format %s, c=%u.\n",
7800 color, tests[t].name, c);
7802 IDirectDrawSurface_Release(texture);
7804 IDirectDrawSurface_Release(src);
7805 IDirectDrawSurface_Release(dst);
7807 done:
7809 destroy_viewport(device, viewport);
7810 destroy_material(green);
7811 IDirectDrawSurface_Release(rt);
7812 IDirect3DExecuteBuffer_Release(execute_buffer);
7813 IDirect3DDevice_Release(device);
7814 refcount = IDirectDraw_Release(ddraw);
7815 ok(refcount == 0, "Ddraw object not properly released, refcount %u.\n", refcount);
7816 DestroyWindow(window);
7819 static void test_range_colorkey(void)
7821 IDirectDraw *ddraw;
7822 HWND window;
7823 HRESULT hr;
7824 IDirectDrawSurface *surface;
7825 DDSURFACEDESC surface_desc;
7826 ULONG refcount;
7827 DDCOLORKEY ckey;
7829 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7830 0, 0, 640, 480, 0, 0, 0, 0);
7831 ddraw = create_ddraw();
7832 ok(!!ddraw, "Failed to create a ddraw object.\n");
7833 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7834 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7836 memset(&surface_desc, 0, sizeof(surface_desc));
7837 surface_desc.dwSize = sizeof(surface_desc);
7838 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
7839 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
7840 surface_desc.dwWidth = 1;
7841 surface_desc.dwHeight = 1;
7842 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
7843 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
7844 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7845 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7846 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x000000ff;
7847 U5(surface_desc.ddpfPixelFormat).dwRGBAlphaBitMask = 0x00000000;
7849 /* Creating a surface with a range color key fails with DDERR_NOCOLORKEY. */
7850 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
7851 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
7852 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7853 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7855 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
7856 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
7857 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7858 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7860 /* Same for DDSCAPS_OFFSCREENPLAIN. */
7861 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7862 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
7863 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000001;
7864 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7865 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7867 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000001;
7868 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
7869 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7870 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7872 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00000000;
7873 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00000000;
7874 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7875 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7877 /* Setting a range color key without DDCKEY_COLORSPACE collapses the key. */
7878 ckey.dwColorSpaceLowValue = 0x00000000;
7879 ckey.dwColorSpaceHighValue = 0x00000001;
7880 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
7881 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
7883 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
7884 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
7885 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
7886 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
7888 ckey.dwColorSpaceLowValue = 0x00000001;
7889 ckey.dwColorSpaceHighValue = 0x00000000;
7890 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT, &ckey);
7891 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
7893 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
7894 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
7895 ok(ckey.dwColorSpaceLowValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
7896 ok(ckey.dwColorSpaceHighValue == 0x00000001, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
7898 /* DDCKEY_COLORSPACE is ignored if the key is a single value. */
7899 ckey.dwColorSpaceLowValue = 0x00000000;
7900 ckey.dwColorSpaceHighValue = 0x00000000;
7901 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
7902 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
7904 /* Using it with a range key results in DDERR_NOCOLORKEYHW. */
7905 ckey.dwColorSpaceLowValue = 0x00000001;
7906 ckey.dwColorSpaceHighValue = 0x00000000;
7907 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
7908 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7909 ckey.dwColorSpaceLowValue = 0x00000000;
7910 ckey.dwColorSpaceHighValue = 0x00000001;
7911 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
7912 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7913 /* Range destination keys don't work either. */
7914 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_DESTBLT | DDCKEY_COLORSPACE, &ckey);
7915 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7917 /* Just to show it's not because of A, R, and G having equal values. */
7918 ckey.dwColorSpaceLowValue = 0x00000000;
7919 ckey.dwColorSpaceHighValue = 0x01010101;
7920 hr = IDirectDrawSurface_SetColorKey(surface, DDCKEY_SRCBLT | DDCKEY_COLORSPACE, &ckey);
7921 ok(hr == DDERR_NOCOLORKEYHW, "Got unexpected hr %#x.\n", hr);
7923 /* None of these operations modified the key. */
7924 hr = IDirectDrawSurface_GetColorKey(surface, DDCKEY_SRCBLT, &ckey);
7925 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
7926 ok(!ckey.dwColorSpaceLowValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceLowValue);
7927 ok(!ckey.dwColorSpaceHighValue, "Got unexpected value 0x%08x.\n", ckey.dwColorSpaceHighValue);
7929 IDirectDrawSurface_Release(surface),
7930 refcount = IDirectDraw_Release(ddraw);
7931 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7932 DestroyWindow(window);
7935 static void test_shademode(void)
7937 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
7938 IDirect3DExecuteBuffer *execute_buffer;
7939 D3DEXECUTEBUFFERDESC exec_desc;
7940 IDirect3DMaterial *background;
7941 IDirect3DViewport *viewport;
7942 IDirect3DDevice *device;
7943 IDirectDrawSurface *rt;
7944 const D3DLVERTEX *quad;
7945 DWORD color0, color1;
7946 UINT i, inst_length;
7947 IDirectDraw *ddraw;
7948 IDirect3D *d3d;
7949 ULONG refcount;
7950 HWND window;
7951 HRESULT hr;
7952 void *ptr;
7953 static const D3DLVERTEX quad_strip[] =
7955 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}},
7956 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff00ff00}},
7957 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xff0000ff}},
7958 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffffffff}},
7960 quad_list[] =
7962 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xff0000ff}},
7963 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}},
7964 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xff00ff00}},
7965 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffffffff}},
7967 static const struct
7969 DWORD primtype;
7970 DWORD shademode;
7971 DWORD color0, color1;
7973 tests[] =
7975 {D3DPT_TRIANGLESTRIP, D3DSHADE_FLAT, 0x00ff0000, 0x000000ff},
7976 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
7977 {D3DPT_TRIANGLESTRIP, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
7978 {D3DPT_TRIANGLESTRIP, D3DSHADE_PHONG, 0x000dca28, 0x000d45c7},
7979 {D3DPT_TRIANGLELIST, D3DSHADE_FLAT, 0x000000ff, 0x0000ff00},
7980 {D3DPT_TRIANGLELIST, D3DSHADE_GOURAUD, 0x000dca28, 0x000d45c7},
7983 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7984 0, 0, 640, 480, 0, 0, 0, 0);
7985 ddraw = create_ddraw();
7986 ok(!!ddraw, "Failed to create a ddraw object.\n");
7987 if (!(device = create_device(ddraw, window, DDSCL_NORMAL)))
7989 skip("Failed to create a 3D device, skipping test.\n");
7990 IDirectDraw_Release(ddraw);
7991 DestroyWindow(window);
7992 return;
7995 hr = IDirect3DDevice_GetDirect3D(device, &d3d);
7996 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
7997 hr = IDirect3DDevice_QueryInterface(device, &IID_IDirectDrawSurface, (void **)&rt);
7998 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
8000 background = create_diffuse_material(device, 1.0f, 1.0f, 1.0f, 1.0f);
8001 viewport = create_viewport(device, 0, 0, 640, 480);
8002 viewport_set_background(device, viewport, background);
8004 memset(&exec_desc, 0, sizeof(exec_desc));
8005 exec_desc.dwSize = sizeof(exec_desc);
8006 exec_desc.dwFlags = D3DDEB_BUFSIZE | D3DDEB_CAPS;
8007 exec_desc.dwBufferSize = 1024;
8008 exec_desc.dwCaps = D3DDEBCAPS_SYSTEMMEMORY;
8010 hr = IDirect3DDevice_CreateExecuteBuffer(device, &exec_desc, &execute_buffer, NULL);
8011 ok(SUCCEEDED(hr), "Failed to create execute buffer, hr %#x.\n", hr);
8013 /* Try it first with a TRIANGLESTRIP. Do it with different geometry because
8014 * the color fixups we have to do for FLAT shading will be dependent on that. */
8016 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
8018 hr = IDirect3DViewport_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
8019 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8021 hr = IDirect3DExecuteBuffer_Lock(execute_buffer, &exec_desc);
8022 ok(SUCCEEDED(hr), "Failed to lock execute buffer, hr %#x.\n", hr);
8024 quad = tests[i].primtype == D3DPT_TRIANGLESTRIP ? quad_strip : quad_list;
8025 memcpy(exec_desc.lpData, quad, sizeof(quad_strip));
8026 ptr = ((BYTE *)exec_desc.lpData) + sizeof(quad_strip);
8027 emit_set_rs(&ptr, D3DRENDERSTATE_CLIPPING, FALSE);
8028 emit_set_rs(&ptr, D3DRENDERSTATE_ZENABLE, FALSE);
8029 emit_set_rs(&ptr, D3DRENDERSTATE_FOGENABLE, FALSE);
8030 emit_set_rs(&ptr, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
8031 emit_set_rs(&ptr, D3DRENDERSTATE_SHADEMODE, tests[i].shademode);
8033 emit_process_vertices(&ptr, D3DPROCESSVERTICES_TRANSFORM, 0, 4);
8034 if (tests[i].primtype == D3DPT_TRIANGLESTRIP)
8035 emit_tquad(&ptr, 0);
8036 else
8037 emit_tquad_tlist(&ptr, 0);
8038 emit_end(&ptr);
8039 inst_length = (BYTE *)ptr - (BYTE *)exec_desc.lpData;
8040 inst_length -= sizeof(quad_strip);
8042 hr = IDirect3DExecuteBuffer_Unlock(execute_buffer);
8043 ok(SUCCEEDED(hr), "Failed to unlock execute buffer, hr %#x.\n", hr);
8045 hr = IDirect3DDevice2_BeginScene(device);
8046 set_execute_data(execute_buffer, 4, sizeof(quad_strip), inst_length);
8047 hr = IDirect3DDevice_Execute(device, execute_buffer, viewport, D3DEXECUTE_CLIPPED);
8048 ok(SUCCEEDED(hr), "Failed to execute exec buffer, hr %#x.\n", hr);
8049 hr = IDirect3DDevice2_EndScene(device);
8050 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8052 color0 = get_surface_color(rt, 100, 100); /* Inside first triangle */
8053 color1 = get_surface_color(rt, 500, 350); /* Inside second triangle */
8055 /* For D3DSHADE_FLAT it should take the color of the first vertex of
8056 * each triangle. This requires EXT_provoking_vertex or similar
8057 * functionality being available. */
8058 /* PHONG should be the same as GOURAUD, since no hardware implements
8059 * this. */
8060 ok(compare_color(color0, tests[i].color0, 1), "Test %u shading has color0 %08x, expected %08x.\n",
8061 i, color0, tests[i].color0);
8062 ok(compare_color(color1, tests[i].color1, 1), "Test %u shading has color1 %08x, expected %08x.\n",
8063 i, color1, tests[i].color1);
8066 IDirect3DExecuteBuffer_Release(execute_buffer);
8067 destroy_viewport(device, viewport);
8068 destroy_material(background);
8069 IDirectDrawSurface_Release(rt);
8070 IDirect3D_Release(d3d);
8071 refcount = IDirect3DDevice_Release(device);
8072 ok(!refcount, "Device has %u references left.\n", refcount);
8073 IDirectDraw_Release(ddraw);
8074 DestroyWindow(window);
8077 static void test_lockrect_invalid(void)
8079 unsigned int i, r;
8080 IDirectDraw *ddraw;
8081 IDirectDrawSurface *surface;
8082 HWND window;
8083 HRESULT hr;
8084 DDSURFACEDESC surface_desc;
8085 DDCAPS hal_caps;
8086 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
8087 static RECT valid[] =
8089 {60, 60, 68, 68},
8090 {60, 60, 60, 68},
8091 {60, 60, 68, 60},
8092 {120, 60, 128, 68},
8093 {60, 120, 68, 128},
8095 static RECT invalid[] =
8097 {68, 60, 60, 68}, /* left > right */
8098 {60, 68, 68, 60}, /* top > bottom */
8099 {-8, 60, 0, 68}, /* left < surface */
8100 {60, -8, 68, 0}, /* top < surface */
8101 {-16, 60, -8, 68}, /* right < surface */
8102 {60, -16, 68, -8}, /* bottom < surface */
8103 {60, 60, 136, 68}, /* right > surface */
8104 {60, 60, 68, 136}, /* bottom > surface */
8105 {136, 60, 144, 68}, /* left > surface */
8106 {60, 136, 68, 144}, /* top > surface */
8108 static const struct
8110 DWORD caps;
8111 const char *name;
8112 HRESULT hr;
8114 resources[] =
8116 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, "sysmem offscreenplain", DDERR_INVALIDPARAMS},
8117 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, "vidmem offscreenplain", DDERR_INVALIDPARAMS},
8118 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, "sysmem texture", DDERR_INVALIDPARAMS},
8119 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, "vidmem texture", DDERR_INVALIDPARAMS},
8122 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8123 0, 0, 640, 480, 0, 0, 0, 0);
8124 ddraw = create_ddraw();
8125 ok(!!ddraw, "Failed to create a ddraw object.\n");
8126 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8127 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8129 memset(&hal_caps, 0, sizeof(hal_caps));
8130 hal_caps.dwSize = sizeof(hal_caps);
8131 hr = IDirectDraw_GetCaps(ddraw, &hal_caps, NULL);
8132 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
8133 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps)
8135 skip("Required surface types not supported, skipping test.\n");
8136 goto done;
8139 for (r = 0; r < sizeof(resources) / sizeof(*resources); ++r)
8141 memset(&surface_desc, 0, sizeof(surface_desc));
8142 surface_desc.dwSize = sizeof(surface_desc);
8143 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
8144 surface_desc.ddsCaps.dwCaps = resources[r].caps;
8145 surface_desc.dwWidth = 128;
8146 surface_desc.dwHeight = 128;
8147 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
8148 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
8149 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 32;
8150 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xff0000;
8151 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x00ff00;
8152 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x0000ff;
8154 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8155 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, type %s.\n", hr, resources[r].name);
8157 hr = IDirectDrawSurface_Lock(surface, NULL, NULL, DDLOCK_WAIT, NULL);
8158 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x, type %s.\n", hr, resources[r].name);
8160 for (i = 0; i < sizeof(valid) / sizeof(*valid); ++i)
8162 RECT *rect = &valid[i];
8164 memset(&surface_desc, 0, sizeof(surface_desc));
8165 surface_desc.dwSize = sizeof(surface_desc);
8167 hr = IDirectDrawSurface_Lock(surface, rect, &surface_desc, DDLOCK_WAIT, NULL);
8168 ok(SUCCEEDED(hr), "Lock failed (%#x) for rect [%d, %d]->[%d, %d], type %s.\n",
8169 hr, rect->left, rect->top, rect->right, rect->bottom, resources[r].name);
8171 hr = IDirectDrawSurface_Unlock(surface, NULL);
8172 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
8175 for (i = 0; i < sizeof(invalid) / sizeof(*invalid); ++i)
8177 RECT *rect = &invalid[i];
8179 memset(&surface_desc, 1, sizeof(surface_desc));
8180 surface_desc.dwSize = sizeof(surface_desc);
8182 hr = IDirectDrawSurface_Lock(surface, rect, &surface_desc, DDLOCK_WAIT, NULL);
8183 ok(hr == resources[r].hr, "Lock returned %#x for rect [%d, %d]->[%d, %d], type %s.\n",
8184 hr, rect->left, rect->top, rect->right, rect->bottom, resources[r].name);
8185 if (SUCCEEDED(hr))
8187 hr = IDirectDrawSurface_Unlock(surface, NULL);
8188 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
8190 else
8191 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8194 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
8195 ok(SUCCEEDED(hr), "Lock(rect = NULL) failed, hr %#x, type %s.\n",
8196 hr, resources[r].name);
8197 hr = IDirectDrawSurface_Lock(surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
8198 ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = NULL) returned %#x, type %s.\n",
8199 hr, resources[r].name);
8200 hr = IDirectDrawSurface_Unlock(surface, NULL);
8201 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
8203 hr = IDirectDrawSurface_Lock(surface, &valid[0], &surface_desc, DDLOCK_WAIT, NULL);
8204 ok(SUCCEEDED(hr), "Lock(rect = [%d, %d]->[%d, %d]) failed (%#x).\n",
8205 valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
8206 hr = IDirectDrawSurface_Lock(surface, &valid[0], &surface_desc, DDLOCK_WAIT, NULL);
8207 ok(hr == DDERR_SURFACEBUSY, "Double lock(rect = [%d, %d]->[%d, %d]) failed (%#x).\n",
8208 valid[0].left, valid[0].top, valid[0].right, valid[0].bottom, hr);
8210 /* Locking a different rectangle returns DD_OK, but it seems to break the surface.
8211 * Afterwards unlocking the surface fails(NULL rectangle or both locked rectangles) */
8213 hr = IDirectDrawSurface_Unlock(surface, NULL);
8214 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, type %s.\n", hr, resources[r].name);
8216 IDirectDrawSurface_Release(surface);
8219 done:
8220 IDirectDraw_Release(ddraw);
8221 DestroyWindow(window);
8224 static void test_yv12_overlay(void)
8226 IDirectDrawSurface *src_surface, *dst_surface;
8227 RECT rect = {13, 17, 14, 18};
8228 unsigned int offset, y;
8229 unsigned char *base;
8230 DDSURFACEDESC desc;
8231 IDirectDraw *ddraw;
8232 HWND window;
8233 HRESULT hr;
8235 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8236 0, 0, 640, 480, 0, 0, 0, 0);
8237 ddraw = create_ddraw();
8238 ok(!!ddraw, "Failed to create a ddraw object.\n");
8239 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8240 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8242 if (!(src_surface = create_overlay(ddraw, 256, 256, MAKEFOURCC('Y','V','1','2'))))
8244 skip("Failed to create a YV12 overlay, skipping test.\n");
8245 goto done;
8248 memset(&desc, 0, sizeof(desc));
8249 desc.dwSize = sizeof(desc);
8250 hr = IDirectDrawSurface_Lock(src_surface, NULL, &desc, DDLOCK_WAIT, NULL);
8251 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8253 ok(desc.dwFlags == (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS | DDSD_PITCH),
8254 "Got unexpected flags %#x.\n", desc.dwFlags);
8255 ok(desc.ddsCaps.dwCaps == (DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM | DDSCAPS_HWCODEC)
8256 || desc.ddsCaps.dwCaps == (DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM),
8257 "Got unexpected caps %#x.\n", desc.ddsCaps.dwCaps);
8258 ok(desc.dwWidth == 256, "Got unexpected width %u.\n", desc.dwWidth);
8259 ok(desc.dwHeight == 256, "Got unexpected height %u.\n", desc.dwHeight);
8260 /* The overlay pitch seems to have 256 byte alignment. */
8261 ok(!(U1(desc).lPitch & 0xff), "Got unexpected pitch %u.\n", U1(desc).lPitch);
8263 /* Fill the surface with some data for the blit test. */
8264 base = desc.lpSurface;
8265 /* Luminance */
8266 for (y = 0; y < desc.dwHeight; ++y)
8268 memset(base + U1(desc).lPitch * y, 0x10, desc.dwWidth);
8270 /* V */
8271 for (; y < desc.dwHeight + desc.dwHeight / 4; ++y)
8273 memset(base + U1(desc).lPitch * y, 0x20, desc.dwWidth);
8275 /* U */
8276 for (; y < desc.dwHeight + desc.dwHeight / 2; ++y)
8278 memset(base + U1(desc).lPitch * y, 0x30, desc.dwWidth);
8281 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
8282 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8284 /* YV12 uses 2x2 blocks with 6 bytes per block (4*Y, 1*U, 1*V). Unlike
8285 * other block-based formats like DXT the entire Y channel is stored in
8286 * one big chunk of memory, followed by the chroma channels. So partial
8287 * locks do not really make sense. Show that they are allowed nevertheless
8288 * and the offset points into the luminance data. */
8289 hr = IDirectDrawSurface_Lock(src_surface, &rect, &desc, DDLOCK_WAIT, NULL);
8290 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8291 offset = ((const unsigned char *)desc.lpSurface - base);
8292 ok(offset == rect.top * U1(desc).lPitch + rect.left, "Got unexpected offset %u, expected %u.\n",
8293 offset, rect.top * U1(desc).lPitch + rect.left);
8294 hr = IDirectDrawSurface_Unlock(src_surface, NULL);
8295 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8297 if (!(dst_surface = create_overlay(ddraw, 256, 256, MAKEFOURCC('Y','V','1','2'))))
8299 /* Windows XP with a Radeon X1600 GPU refuses to create a second
8300 * overlay surface, DDERR_NOOVERLAYHW, making the blit tests moot. */
8301 skip("Failed to create a second YV12 surface, skipping blit test.\n");
8302 IDirectDrawSurface_Release(src_surface);
8303 goto done;
8306 hr = IDirectDrawSurface_Blt(dst_surface, NULL, src_surface, NULL, DDBLT_WAIT, NULL);
8307 /* VMware rejects YV12 blits. This behavior has not been seen on real
8308 * hardware yet, so mark it broken. */
8309 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL), "Failed to blit, hr %#x.\n", hr);
8311 if (SUCCEEDED(hr))
8313 memset(&desc, 0, sizeof(desc));
8314 desc.dwSize = sizeof(desc);
8315 hr = IDirectDrawSurface_Lock(dst_surface, NULL, &desc, DDLOCK_WAIT, NULL);
8316 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8318 base = desc.lpSurface;
8319 ok(base[0] == 0x10, "Got unexpected Y data 0x%02x.\n", base[0]);
8320 base += desc.dwHeight * U1(desc).lPitch;
8321 todo_wine ok(base[0] == 0x20, "Got unexpected V data 0x%02x.\n", base[0]);
8322 base += desc.dwHeight / 4 * U1(desc).lPitch;
8323 todo_wine ok(base[0] == 0x30, "Got unexpected U data 0x%02x.\n", base[0]);
8325 hr = IDirectDrawSurface_Unlock(dst_surface, NULL);
8326 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8329 IDirectDrawSurface_Release(dst_surface);
8330 IDirectDrawSurface_Release(src_surface);
8331 done:
8332 IDirectDraw_Release(ddraw);
8333 DestroyWindow(window);
8336 static void test_offscreen_overlay(void)
8338 IDirectDrawSurface *overlay, *offscreen, *primary;
8339 DDSURFACEDESC surface_desc;
8340 IDirectDraw *ddraw;
8341 HWND window;
8342 HRESULT hr;
8343 HDC dc;
8345 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8346 0, 0, 640, 480, 0, 0, 0, 0);
8347 ddraw = create_ddraw();
8348 ok(!!ddraw, "Failed to create a ddraw object.\n");
8349 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8350 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8352 if (!(overlay = create_overlay(ddraw, 64, 64, MAKEFOURCC('U','Y','V','Y'))))
8354 skip("Failed to create a UYVY overlay, skipping test.\n");
8355 goto done;
8358 memset(&surface_desc, 0, sizeof(surface_desc));
8359 surface_desc.dwSize = sizeof(surface_desc);
8360 surface_desc.dwFlags = DDSD_CAPS;
8361 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
8362 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary, NULL);
8363 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
8365 /* On Windows 7, and probably Vista, UpdateOverlay() will return
8366 * DDERR_OUTOFCAPS if the dwm is active. Calling GetDC() on the primary
8367 * surface prevents this by disabling the dwm. */
8368 hr = IDirectDrawSurface_GetDC(primary, &dc);
8369 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
8370 hr = IDirectDrawSurface_ReleaseDC(primary, dc);
8371 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
8373 /* Try to overlay a NULL surface. */
8374 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, NULL, NULL, DDOVER_SHOW, NULL);
8375 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8376 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, NULL, NULL, DDOVER_HIDE, NULL);
8377 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
8379 /* Try to overlay an offscreen surface. */
8380 memset(&surface_desc, 0, sizeof(surface_desc));
8381 surface_desc.dwSize = sizeof(surface_desc);
8382 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
8383 surface_desc.dwWidth = 64;
8384 surface_desc.dwHeight = 64;
8385 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8386 surface_desc.ddpfPixelFormat.dwSize = sizeof(surface_desc.ddpfPixelFormat);
8387 surface_desc.ddpfPixelFormat.dwFlags = DDPF_RGB;
8388 surface_desc.ddpfPixelFormat.dwFourCC = 0;
8389 U1(surface_desc.ddpfPixelFormat).dwRGBBitCount = 16;
8390 U2(surface_desc.ddpfPixelFormat).dwRBitMask = 0xf800;
8391 U3(surface_desc.ddpfPixelFormat).dwGBitMask = 0x07e0;
8392 U4(surface_desc.ddpfPixelFormat).dwBBitMask = 0x001f;
8393 hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &offscreen, NULL);
8394 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
8396 hr = IDirectDrawSurface_UpdateOverlay(overlay, NULL, offscreen, NULL, DDOVER_SHOW, NULL);
8397 ok(SUCCEEDED(hr), "Failed to update overlay, hr %#x.\n", hr);
8399 /* Try to overlay the primary with a non-overlay surface. */
8400 hr = IDirectDrawSurface_UpdateOverlay(offscreen, NULL, primary, NULL, DDOVER_SHOW, NULL);
8401 ok(hr == DDERR_NOTAOVERLAYSURFACE, "Got unexpected hr %#x.\n", hr);
8402 hr = IDirectDrawSurface_UpdateOverlay(offscreen, NULL, primary, NULL, DDOVER_HIDE, NULL);
8403 ok(hr == DDERR_NOTAOVERLAYSURFACE, "Got unexpected hr %#x.\n", hr);
8405 IDirectDrawSurface_Release(offscreen);
8406 IDirectDrawSurface_Release(primary);
8407 IDirectDrawSurface_Release(overlay);
8408 done:
8409 IDirectDraw_Release(ddraw);
8410 DestroyWindow(window);
8413 START_TEST(ddraw1)
8415 IDirectDraw *ddraw;
8416 DEVMODEW current_mode;
8418 if (!(ddraw = create_ddraw()))
8420 skip("Failed to create a ddraw object, skipping tests.\n");
8421 return;
8423 IDirectDraw_Release(ddraw);
8425 memset(&current_mode, 0, sizeof(current_mode));
8426 current_mode.dmSize = sizeof(current_mode);
8427 ok(EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &current_mode), "Failed to get display mode.\n");
8428 registry_mode.dmSize = sizeof(registry_mode);
8429 ok(EnumDisplaySettingsW(NULL, ENUM_REGISTRY_SETTINGS, &registry_mode), "Failed to get display mode.\n");
8430 if (registry_mode.dmPelsWidth != current_mode.dmPelsWidth
8431 || registry_mode.dmPelsHeight != current_mode.dmPelsHeight)
8433 skip("Current mode does not match registry mode, skipping test.\n");
8434 return;
8437 test_coop_level_create_device_window();
8438 test_clipper_blt();
8439 test_coop_level_d3d_state();
8440 test_surface_interface_mismatch();
8441 test_coop_level_threaded();
8442 test_viewport();
8443 test_zenable();
8444 test_ck_rgba();
8445 test_ck_default();
8446 test_ck_complex();
8447 test_surface_qi();
8448 test_device_qi();
8449 test_wndproc();
8450 test_window_style();
8451 test_redundant_mode_set();
8452 test_coop_level_mode_set();
8453 test_coop_level_mode_set_multi();
8454 test_initialize();
8455 test_coop_level_surf_create();
8456 test_coop_level_multi_window();
8457 test_clear_rect_count();
8458 test_coop_level_activateapp();
8459 test_unsupported_formats();
8460 test_rt_caps();
8461 test_primary_caps();
8462 test_surface_lock();
8463 test_surface_discard();
8464 test_flip();
8465 test_sysmem_overlay();
8466 test_primary_palette();
8467 test_surface_attachment();
8468 test_pixel_format();
8469 test_create_surface_pitch();
8470 test_mipmap();
8471 test_palette_complex();
8472 test_p8_rgb_blit();
8473 test_material();
8474 test_lighting();
8475 test_palette_gdi();
8476 test_palette_alpha();
8477 test_lost_device();
8478 test_surface_desc_lock();
8479 test_texturemapblend();
8480 test_viewport_clear_rect();
8481 test_color_fill();
8482 test_colorkey_precision();
8483 test_range_colorkey();
8484 test_shademode();
8485 test_lockrect_invalid();
8486 test_yv12_overlay();
8487 test_offscreen_overlay();