ddraw/tests: Add a test for signed formats.
[wine/multimedia.git] / dlls / ddraw / tests / ddraw4.c
blob87caf78fa716840dc56d933cdeb7e4da451b1ee8
1 /*
2 * Copyright 2011-2014 Henri Verbeet for CodeWeavers
3 * Copyright 2012-2014 Stefan Dösinger for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define COBJMACROS
21 #include "wine/test.h"
22 #include <limits.h>
23 #include "d3d.h"
25 static DEVMODEW registry_mode;
27 struct vec2
29 float x, y;
32 struct vec3
34 float x, y, z;
37 struct vec4
39 float x, y, z, w;
42 struct create_window_thread_param
44 HWND window;
45 HANDLE window_created;
46 HANDLE destroy_window;
47 HANDLE thread;
50 static BOOL compare_float(float f, float g, unsigned int ulps)
52 int x = *(int *)&f;
53 int y = *(int *)&g;
55 if (x < 0)
56 x = INT_MIN - x;
57 if (y < 0)
58 y = INT_MIN - y;
60 if (abs(x - y) > ulps)
61 return FALSE;
63 return TRUE;
66 static BOOL compare_vec4(struct vec4 *vec, float x, float y, float z, float w, unsigned int ulps)
68 return compare_float(vec->x, x, ulps)
69 && compare_float(vec->y, y, ulps)
70 && compare_float(vec->z, z, ulps)
71 && compare_float(vec->w, w, ulps);
74 static BOOL compare_color(D3DCOLOR c1, D3DCOLOR c2, BYTE max_diff)
76 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
77 c1 >>= 8; c2 >>= 8;
78 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
79 c1 >>= 8; c2 >>= 8;
80 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
81 c1 >>= 8; c2 >>= 8;
82 if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) return FALSE;
83 return TRUE;
86 static DWORD WINAPI create_window_thread_proc(void *param)
88 struct create_window_thread_param *p = param;
89 DWORD res;
90 BOOL ret;
92 p->window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
93 0, 0, 640, 480, 0, 0, 0, 0);
94 ret = SetEvent(p->window_created);
95 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
97 for (;;)
99 MSG msg;
101 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
102 DispatchMessageA(&msg);
103 res = WaitForSingleObject(p->destroy_window, 100);
104 if (res == WAIT_OBJECT_0)
105 break;
106 if (res != WAIT_TIMEOUT)
108 ok(0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
109 break;
113 DestroyWindow(p->window);
115 return 0;
118 static void create_window_thread(struct create_window_thread_param *p)
120 DWORD res, tid;
122 p->window_created = CreateEventA(NULL, FALSE, FALSE, NULL);
123 ok(!!p->window_created, "CreateEvent failed, last error %#x.\n", GetLastError());
124 p->destroy_window = CreateEventA(NULL, FALSE, FALSE, NULL);
125 ok(!!p->destroy_window, "CreateEvent failed, last error %#x.\n", GetLastError());
126 p->thread = CreateThread(NULL, 0, create_window_thread_proc, p, 0, &tid);
127 ok(!!p->thread, "Failed to create thread, last error %#x.\n", GetLastError());
128 res = WaitForSingleObject(p->window_created, INFINITE);
129 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
132 static void destroy_window_thread(struct create_window_thread_param *p)
134 SetEvent(p->destroy_window);
135 WaitForSingleObject(p->thread, INFINITE);
136 CloseHandle(p->destroy_window);
137 CloseHandle(p->window_created);
138 CloseHandle(p->thread);
141 static IDirectDrawSurface4 *get_depth_stencil(IDirect3DDevice3 *device)
143 IDirectDrawSurface4 *rt, *ret;
144 DDSCAPS2 caps = {DDSCAPS_ZBUFFER, 0, 0, {0}};
145 HRESULT hr;
147 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
148 ok(SUCCEEDED(hr), "Failed to get the render target, hr %#x.\n", hr);
149 hr = IDirectDrawSurface4_GetAttachedSurface(rt, &caps, &ret);
150 ok(SUCCEEDED(hr) || hr == DDERR_NOTFOUND, "Failed to get the z buffer, hr %#x.\n", hr);
151 IDirectDrawSurface4_Release(rt);
152 return ret;
155 static HRESULT set_display_mode(IDirectDraw4 *ddraw, DWORD width, DWORD height)
157 if (SUCCEEDED(IDirectDraw4_SetDisplayMode(ddraw, width, height, 32, 0, 0)))
158 return DD_OK;
159 return IDirectDraw4_SetDisplayMode(ddraw, width, height, 24, 0, 0);
162 static D3DCOLOR get_surface_color(IDirectDrawSurface4 *surface, UINT x, UINT y)
164 RECT rect = {x, y, x + 1, y + 1};
165 DDSURFACEDESC2 surface_desc;
166 D3DCOLOR color;
167 HRESULT hr;
169 memset(&surface_desc, 0, sizeof(surface_desc));
170 surface_desc.dwSize = sizeof(surface_desc);
172 hr = IDirectDrawSurface4_Lock(surface, &rect, &surface_desc, DDLOCK_READONLY | DDLOCK_WAIT, NULL);
173 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
174 if (FAILED(hr))
175 return 0xdeadbeef;
177 color = *((DWORD *)surface_desc.lpSurface) & 0x00ffffff;
179 hr = IDirectDrawSurface4_Unlock(surface, &rect);
180 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
182 return color;
185 static HRESULT CALLBACK enum_z_fmt(DDPIXELFORMAT *format, void *ctx)
187 DDPIXELFORMAT *z_fmt = ctx;
189 if (U1(*format).dwZBufferBitDepth > U1(*z_fmt).dwZBufferBitDepth)
190 *z_fmt = *format;
192 return DDENUMRET_OK;
195 static IDirectDraw4 *create_ddraw(void)
197 IDirectDraw4 *ddraw4;
198 IDirectDraw *ddraw1;
199 HRESULT hr;
201 if (FAILED(DirectDrawCreate(NULL, &ddraw1, NULL)))
202 return NULL;
204 hr = IDirectDraw_QueryInterface(ddraw1, &IID_IDirectDraw4, (void **)&ddraw4);
205 IDirectDraw_Release(ddraw1);
206 if (FAILED(hr))
207 return NULL;
209 return ddraw4;
212 static IDirect3DDevice3 *create_device(HWND window, DWORD coop_level)
214 IDirectDrawSurface4 *surface, *ds;
215 IDirect3DDevice3 *device = NULL;
216 DDSURFACEDESC2 surface_desc;
217 IDirectDraw4 *ddraw4;
218 DDPIXELFORMAT z_fmt;
219 IDirect3D3 *d3d3;
220 HRESULT hr;
222 if (!(ddraw4 = create_ddraw()))
223 return NULL;
225 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, coop_level);
226 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
228 memset(&surface_desc, 0, sizeof(surface_desc));
229 surface_desc.dwSize = sizeof(surface_desc);
230 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
231 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
232 surface_desc.dwWidth = 640;
233 surface_desc.dwHeight = 480;
235 hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &surface, NULL);
236 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
238 if (coop_level & DDSCL_NORMAL)
240 IDirectDrawClipper *clipper;
242 hr = IDirectDraw4_CreateClipper(ddraw4, 0, &clipper, NULL);
243 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
244 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
245 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
246 hr = IDirectDrawSurface4_SetClipper(surface, clipper);
247 ok(SUCCEEDED(hr), "Failed to set surface clipper, hr %#x.\n", hr);
248 IDirectDrawClipper_Release(clipper);
251 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirect3D3, (void **)&d3d3);
252 IDirectDraw4_Release(ddraw4);
253 if (FAILED(hr))
255 IDirectDrawSurface4_Release(surface);
256 return NULL;
259 memset(&z_fmt, 0, sizeof(z_fmt));
260 hr = IDirect3D3_EnumZBufferFormats(d3d3, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
261 if (FAILED(hr) || !z_fmt.dwSize)
263 IDirect3D3_Release(d3d3);
264 IDirectDrawSurface4_Release(surface);
265 return NULL;
268 memset(&surface_desc, 0, sizeof(surface_desc));
269 surface_desc.dwSize = sizeof(surface_desc);
270 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
271 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
272 U4(surface_desc).ddpfPixelFormat = z_fmt;
273 surface_desc.dwWidth = 640;
274 surface_desc.dwHeight = 480;
275 hr = IDirectDraw4_CreateSurface(ddraw4, &surface_desc, &ds, NULL);
276 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
277 if (FAILED(hr))
279 IDirect3D3_Release(d3d3);
280 IDirectDrawSurface4_Release(surface);
281 return NULL;
284 hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
285 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
286 IDirectDrawSurface4_Release(ds);
287 if (FAILED(hr))
289 IDirect3D3_Release(d3d3);
290 IDirectDrawSurface4_Release(surface);
291 return NULL;
294 hr = IDirect3D3_CreateDevice(d3d3, &IID_IDirect3DHALDevice, surface, &device, NULL);
295 IDirect3D3_Release(d3d3);
296 IDirectDrawSurface4_Release(surface);
297 if (FAILED(hr))
298 return NULL;
300 return device;
303 static IDirect3DViewport3 *create_viewport(IDirect3DDevice3 *device, UINT x, UINT y, UINT w, UINT h)
305 IDirect3DViewport3 *viewport;
306 D3DVIEWPORT2 vp;
307 IDirect3D3 *d3d;
308 HRESULT hr;
310 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
311 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
312 hr = IDirect3D3_CreateViewport(d3d, &viewport, NULL);
313 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
314 hr = IDirect3DDevice3_AddViewport(device, viewport);
315 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
316 memset(&vp, 0, sizeof(vp));
317 vp.dwSize = sizeof(vp);
318 vp.dwX = x;
319 vp.dwY = y;
320 vp.dwWidth = w;
321 vp.dwHeight = h;
322 vp.dvClipX = -1.0f;
323 vp.dvClipY = 1.0f;
324 vp.dvClipWidth = 2.0f;
325 vp.dvClipHeight = 2.0f;
326 vp.dvMinZ = 0.0f;
327 vp.dvMaxZ = 1.0f;
328 hr = IDirect3DViewport3_SetViewport2(viewport, &vp);
329 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
330 IDirect3D3_Release(d3d);
332 return viewport;
335 static void destroy_viewport(IDirect3DDevice3 *device, IDirect3DViewport3 *viewport)
337 HRESULT hr;
339 hr = IDirect3DDevice3_DeleteViewport(device, viewport);
340 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
341 IDirect3DViewport3_Release(viewport);
344 static IDirect3DMaterial3 *create_material(IDirect3DDevice3 *device, D3DMATERIAL *mat)
346 IDirect3DMaterial3 *material;
347 IDirect3D3 *d3d;
348 HRESULT hr;
350 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
351 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
352 hr = IDirect3D3_CreateMaterial(d3d, &material, NULL);
353 ok(SUCCEEDED(hr), "Failed to create material, hr %#x.\n", hr);
354 hr = IDirect3DMaterial3_SetMaterial(material, mat);
355 ok(SUCCEEDED(hr), "Failed to set material data, hr %#x.\n", hr);
356 IDirect3D3_Release(d3d);
358 return material;
361 static IDirect3DMaterial3 *create_diffuse_material(IDirect3DDevice3 *device, float r, float g, float b, float a)
363 D3DMATERIAL mat;
365 memset(&mat, 0, sizeof(mat));
366 mat.dwSize = sizeof(mat);
367 U1(U(mat).diffuse).r = r;
368 U2(U(mat).diffuse).g = g;
369 U3(U(mat).diffuse).b = b;
370 U4(U(mat).diffuse).a = a;
372 return create_material(device, &mat);
375 static IDirect3DMaterial3 *create_emissive_material(IDirect3DDevice3 *device, float r, float g, float b, float a)
377 D3DMATERIAL mat;
379 memset(&mat, 0, sizeof(mat));
380 mat.dwSize = sizeof(mat);
381 U1(U3(mat).emissive).r = r;
382 U2(U3(mat).emissive).g = g;
383 U3(U3(mat).emissive).b = b;
384 U4(U3(mat).emissive).a = a;
386 return create_material(device, &mat);
389 static void destroy_material(IDirect3DMaterial3 *material)
391 IDirect3DMaterial3_Release(material);
394 struct message
396 UINT message;
397 BOOL check_wparam;
398 WPARAM expect_wparam;
401 static const struct message *expect_messages;
403 static LRESULT CALLBACK test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
405 if (expect_messages && message == expect_messages->message)
407 if (expect_messages->check_wparam)
408 ok (wparam == expect_messages->expect_wparam,
409 "Got unexpected wparam %lx for message %x, expected %lx.\n",
410 wparam, message, expect_messages->expect_wparam);
412 ++expect_messages;
415 return DefWindowProcA(hwnd, message, wparam, lparam);
418 /* Set the wndproc back to what ddraw expects it to be, and release the ddraw
419 * interface. This prevents subsequent SetCooperativeLevel() calls on a
420 * different window from failing with DDERR_HWNDALREADYSET. */
421 static void fix_wndproc(HWND window, LONG_PTR proc)
423 IDirectDraw4 *ddraw;
424 HRESULT hr;
426 if (!(ddraw = create_ddraw()))
427 return;
429 SetWindowLongPtrA(window, GWLP_WNDPROC, proc);
430 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
431 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
432 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
433 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
435 IDirectDraw4_Release(ddraw);
438 static void test_process_vertices(void)
440 IDirect3DVertexBuffer *src_vb, *dst_vb;
441 IDirect3DViewport3 *viewport;
442 D3DVERTEXBUFFERDESC vb_desc;
443 IDirect3DDevice3 *device;
444 struct vec3 *src_data;
445 struct vec4 *dst_data;
446 IDirect3D3 *d3d3;
447 D3DVIEWPORT2 vp2;
448 D3DVIEWPORT vp1;
449 HWND window;
450 HRESULT hr;
452 static D3DMATRIX identity =
454 1.0f, 0.0f, 0.0f, 0.0f,
455 0.0f, 1.0f, 0.0f, 0.0f,
456 0.0f, 0.0f, 1.0f, 0.0f,
457 0.0f, 0.0f, 0.0f, 1.0f,
459 static D3DMATRIX projection =
461 1.0f, 0.0f, 0.0f, 0.0f,
462 0.0f, 1.0f, 0.0f, 0.0f,
463 0.0f, 0.0f, 1.0f, 0.0f,
464 6.0f, 7.0f, 8.0f, 1.0f,
467 window = CreateWindowA("static", "d3d7_test", WS_OVERLAPPEDWINDOW,
468 0, 0, 640, 480, 0, 0, 0, 0);
469 if (!(device = create_device(window, DDSCL_NORMAL)))
471 skip("Failed to create a 3D device, skipping test.\n");
472 DestroyWindow(window);
473 return;
476 hr = IDirect3DDevice3_GetDirect3D(device, &d3d3);
477 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
479 memset(&vb_desc, 0, sizeof(vb_desc));
480 vb_desc.dwSize = sizeof(vb_desc);
481 vb_desc.dwFVF = D3DFVF_XYZ;
482 vb_desc.dwNumVertices = 3;
483 hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &src_vb, 0, NULL);
484 ok(SUCCEEDED(hr), "Failed to create source vertex buffer, hr %#x.\n", hr);
486 hr = IDirect3DVertexBuffer_Lock(src_vb, DDLOCK_WRITEONLY, (void **)&src_data, NULL);
487 ok(SUCCEEDED(hr), "Failed to lock source vertex buffer, hr %#x.\n", hr);
488 src_data[0].x = -1.0f;
489 src_data[0].y = -1.0f;
490 src_data[0].z = -1.0f;
491 src_data[1].x = 0.0f;
492 src_data[1].y = 0.0f;
493 src_data[1].z = 0.0f;
494 src_data[2].x = 1.0f;
495 src_data[2].y = 1.0f;
496 src_data[2].z = 1.0f;
497 hr = IDirect3DVertexBuffer_Unlock(src_vb);
498 ok(SUCCEEDED(hr), "Failed to unlock source vertex buffer, hr %#x.\n", hr);
500 memset(&vb_desc, 0, sizeof(vb_desc));
501 vb_desc.dwSize = sizeof(vb_desc);
502 vb_desc.dwFVF = D3DFVF_XYZRHW;
503 vb_desc.dwNumVertices = 3;
504 hr = IDirect3D3_CreateVertexBuffer(d3d3, &vb_desc, &dst_vb, 0, NULL);
505 ok(SUCCEEDED(hr), "Failed to create destination vertex buffer, hr %#x.\n", hr);
507 hr = IDirect3D3_CreateViewport(d3d3, &viewport, NULL);
508 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
509 hr = IDirect3DDevice3_AddViewport(device, viewport);
510 ok(SUCCEEDED(hr), "Failed to add viewport, hr %#x.\n", hr);
511 vp2.dwSize = sizeof(vp2);
512 vp2.dwX = 10;
513 vp2.dwY = 20;
514 vp2.dwWidth = 100;
515 vp2.dwHeight = 200;
516 vp2.dvClipX = 2.0f;
517 vp2.dvClipY = 3.0f;
518 vp2.dvClipWidth = 4.0f;
519 vp2.dvClipHeight = 5.0f;
520 vp2.dvMinZ = -2.0f;
521 vp2.dvMaxZ = 3.0f;
522 hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
523 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
524 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
525 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
527 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_WORLD, &identity);
528 ok(SUCCEEDED(hr), "Failed to set world transformation, hr %#x.\n", hr);
529 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_VIEW, &identity);
530 ok(SUCCEEDED(hr), "Failed to set view transformation, hr %#x.\n", hr);
531 hr = IDirect3DDevice3_SetTransform(device, D3DTRANSFORMSTATE_PROJECTION, &identity);
532 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
534 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
535 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
537 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
538 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
539 ok(compare_vec4(&dst_data[0], -6.500e+1f, +1.800e+2f, +2.000e-1f, +1.000e+0f, 4096),
540 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
541 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
542 ok(compare_vec4(&dst_data[1], -4.000e+1f, +1.400e+2f, +4.000e-1f, +1.000e+0f, 4096),
543 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
544 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
545 ok(compare_vec4(&dst_data[2], -1.500e+1f, +1.000e+2f, +6.000e-1f, +1.000e+0f, 4096),
546 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
547 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
548 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
549 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
551 hr = IDirect3DDevice3_MultiplyTransform(device, D3DTRANSFORMSTATE_PROJECTION, &projection);
552 ok(SUCCEEDED(hr), "Failed to set projection transformation, hr %#x.\n", hr);
554 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
555 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
557 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
558 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
559 ok(compare_vec4(&dst_data[0], +8.500e+1f, -1.000e+2f, +1.800e+0f, +1.000e+0f, 4096),
560 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
561 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
562 ok(compare_vec4(&dst_data[1], +1.100e+2f, -1.400e+2f, +2.000e+0f, +1.000e+0f, 4096),
563 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
564 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
565 ok(compare_vec4(&dst_data[2], +1.350e+2f, -1.800e+2f, +2.200e+0f, +1.000e+0f, 4096),
566 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
567 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
568 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
569 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
571 vp2.dwSize = sizeof(vp2);
572 vp2.dwX = 30;
573 vp2.dwY = 40;
574 vp2.dwWidth = 90;
575 vp2.dwHeight = 80;
576 vp2.dvClipX = 4.0f;
577 vp2.dvClipY = 6.0f;
578 vp2.dvClipWidth = 2.0f;
579 vp2.dvClipHeight = 4.0f;
580 vp2.dvMinZ = 3.0f;
581 vp2.dvMaxZ = -2.0f;
582 hr = IDirect3DViewport3_SetViewport2(viewport, &vp2);
583 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
585 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
586 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
588 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
589 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
590 ok(compare_vec4(&dst_data[0], +7.500e+1f, +4.000e+1f, -8.000e-1f, +1.000e+0f, 4096),
591 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
592 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
593 ok(compare_vec4(&dst_data[1], +1.200e+2f, +2.000e+1f, -1.000e+0f, +1.000e+0f, 4096),
594 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
595 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
596 ok(compare_vec4(&dst_data[2], +1.650e+2f, +0.000e+0f, -1.200e+0f, +1.000e+0f, 4096),
597 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
598 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
599 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
600 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
602 vp1.dwSize = sizeof(vp1);
603 vp1.dwX = 30;
604 vp1.dwY = 40;
605 vp1.dwWidth = 90;
606 vp1.dwHeight = 80;
607 vp1.dvScaleX = 7.0f;
608 vp1.dvScaleY = 2.0f;
609 vp1.dvMaxX = 6.0f;
610 vp1.dvMaxY = 10.0f;
611 vp1.dvMinZ = -2.0f;
612 vp1.dvMaxZ = 3.0f;
613 hr = IDirect3DViewport3_SetViewport(viewport, &vp1);
614 ok(SUCCEEDED(hr), "Failed to set viewport data, hr %#x.\n", hr);
616 hr = IDirect3DVertexBuffer_ProcessVertices(dst_vb, D3DVOP_TRANSFORM, 0, 3, src_vb, 0, device, 0);
617 ok(SUCCEEDED(hr), "Failed to process vertices, hr %#x.\n", hr);
619 hr = IDirect3DVertexBuffer_Lock(dst_vb, DDLOCK_READONLY, (void **)&dst_data, NULL);
620 ok(SUCCEEDED(hr), "Failed to lock destination vertex buffer, hr %#x.\n", hr);
621 ok(compare_vec4(&dst_data[0], +1.100e+2f, +6.800e+1f, +7.000e+0f, +1.000e+0f, 4096),
622 "Got unexpected vertex 0 {%.8e, %.8e, %.8e, %.8e}.\n",
623 dst_data[0].x, dst_data[0].y, dst_data[0].z, dst_data[0].w);
624 ok(compare_vec4(&dst_data[1], +1.170e+2f, +6.600e+1f, +8.000e+0f, +1.000e+0f, 4096),
625 "Got unexpected vertex 1 {%.8e, %.8e, %.8e, %.8e}.\n",
626 dst_data[1].x, dst_data[1].y, dst_data[1].z, dst_data[1].w);
627 ok(compare_vec4(&dst_data[2], +1.240e+2f, +6.400e+1f, +9.000e+0f, +1.000e+0f, 4096),
628 "Got unexpected vertex 2 {%.8e, %.8e, %.8e, %.8e}.\n",
629 dst_data[2].x, dst_data[2].y, dst_data[2].z, dst_data[2].w);
630 hr = IDirect3DVertexBuffer_Unlock(dst_vb);
631 ok(SUCCEEDED(hr), "Failed to unlock destination vertex buffer, hr %#x.\n", hr);
633 hr = IDirect3DDevice3_DeleteViewport(device, viewport);
634 ok(SUCCEEDED(hr), "Failed to delete viewport, hr %#x.\n", hr);
636 IDirect3DVertexBuffer_Release(dst_vb);
637 IDirect3DVertexBuffer_Release(src_vb);
638 IDirect3DViewport3_Release(viewport);
639 IDirect3D3_Release(d3d3);
640 IDirect3DDevice3_Release(device);
641 DestroyWindow(window);
644 static void test_coop_level_create_device_window(void)
646 HWND focus_window, device_window;
647 IDirectDraw4 *ddraw;
648 HRESULT hr;
650 focus_window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
651 0, 0, 640, 480, 0, 0, 0, 0);
652 ddraw = create_ddraw();
653 ok(!!ddraw, "Failed to create a ddraw object.\n");
655 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
656 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
657 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
658 ok(!device_window, "Unexpected device window found.\n");
659 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW);
660 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
661 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
662 ok(!device_window, "Unexpected device window found.\n");
663 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL);
664 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
665 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
666 ok(!device_window, "Unexpected device window found.\n");
667 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_NORMAL | DDSCL_FULLSCREEN);
668 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
669 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
670 ok(!device_window, "Unexpected device window found.\n");
671 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
672 ok(hr == DDERR_NOFOCUSWINDOW || broken(hr == DDERR_INVALIDPARAMS), "Got unexpected hr %#x.\n", hr);
673 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
674 ok(!device_window, "Unexpected device window found.\n");
676 /* Windows versions before 98 / NT5 don't support DDSCL_CREATEDEVICEWINDOW. */
677 if (broken(hr == DDERR_INVALIDPARAMS))
679 win_skip("DDSCL_CREATEDEVICEWINDOW not supported, skipping test.\n");
680 IDirectDraw4_Release(ddraw);
681 DestroyWindow(focus_window);
682 return;
685 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
686 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
687 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
688 ok(!device_window, "Unexpected device window found.\n");
689 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
690 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
691 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
692 ok(!device_window, "Unexpected device window found.\n");
694 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
695 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
696 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
697 ok(!device_window, "Unexpected device window found.\n");
698 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_SETFOCUSWINDOW
699 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
700 ok(hr == DDERR_NOHWND, "Got unexpected hr %#x.\n", hr);
701 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
702 ok(!!device_window, "Device window not found.\n");
704 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
705 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
706 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
707 ok(!device_window, "Unexpected device window found.\n");
708 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW
709 | DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
710 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
711 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
712 ok(!!device_window, "Device window not found.\n");
714 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
715 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
716 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
717 ok(!device_window, "Unexpected device window found.\n");
718 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
719 ok(hr == DDERR_NOFOCUSWINDOW, "Got unexpected hr %#x.\n", hr);
720 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
721 ok(!device_window, "Unexpected device window found.\n");
722 hr = IDirectDraw4_SetCooperativeLevel(ddraw, focus_window, DDSCL_SETFOCUSWINDOW);
723 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
724 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
725 ok(!device_window, "Unexpected device window found.\n");
726 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_CREATEDEVICEWINDOW | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
727 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
728 device_window = FindWindowA("DirectDrawDeviceWnd", "DirectDrawDeviceWnd");
729 ok(!!device_window, "Device window not found.\n");
731 IDirectDraw4_Release(ddraw);
732 DestroyWindow(focus_window);
735 static void test_clipper_blt(void)
737 IDirectDrawSurface4 *src_surface, *dst_surface;
738 RECT client_rect, src_rect;
739 IDirectDrawClipper *clipper;
740 DDSURFACEDESC2 surface_desc;
741 unsigned int i, j, x, y;
742 IDirectDraw4 *ddraw;
743 RGNDATA *rgn_data;
744 D3DCOLOR color;
745 HRGN r1, r2;
746 HWND window;
747 DDBLTFX fx;
748 HRESULT hr;
749 DWORD *ptr;
750 DWORD ret;
752 static const DWORD src_data[] =
754 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
755 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
756 0xff0000ff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffffffff, 0xffffffff,
758 static const D3DCOLOR expected1[] =
760 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
761 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000,
762 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
763 0x00000000, 0x00000000, 0x00ff0000, 0x00ffffff,
765 /* Nvidia on Windows seems to have an off-by-one error
766 * when processing source rectangles. Our left = 1 and
767 * right = 5 input reads from x = {1, 2, 3}. x = 4 is
768 * read as well, but only for the edge pixels on the
769 * output image. The bug happens on the y axis as well,
770 * but we only read one row there, and all source rows
771 * contain the same data. This bug is not dependent on
772 * the presence of a clipper. */
773 static const D3DCOLOR expected1_broken[] =
775 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
776 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
777 0x00000000, 0x00000000, 0x00ff0000, 0x00ff0000,
778 0x00000000, 0x00000000, 0x0000ff00, 0x00ff0000,
780 static const D3DCOLOR expected2[] =
782 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
783 0x000000ff, 0x000000ff, 0x00000000, 0x00000000,
784 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
785 0x00000000, 0x00000000, 0x000000ff, 0x000000ff,
788 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
789 10, 10, 640, 480, 0, 0, 0, 0);
790 ShowWindow(window, SW_SHOW);
791 ddraw = create_ddraw();
792 ok(!!ddraw, "Failed to create a ddraw object.\n");
794 ret = GetClientRect(window, &client_rect);
795 ok(ret, "Failed to get client rect.\n");
796 ret = MapWindowPoints(window, NULL, (POINT *)&client_rect, 2);
797 ok(ret, "Failed to map client rect.\n");
799 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
800 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
802 hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
803 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
804 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
805 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
806 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
807 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
808 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
809 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
810 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
811 hr = IDirectDrawClipper_GetClipList(clipper, NULL, rgn_data, &ret);
812 ok(SUCCEEDED(hr), "Failed to get clip list, hr %#x.\n", hr);
813 ok(rgn_data->rdh.dwSize == sizeof(rgn_data->rdh), "Got unexpected structure size %#x.\n", rgn_data->rdh.dwSize);
814 ok(rgn_data->rdh.iType == RDH_RECTANGLES, "Got unexpected type %#x.\n", rgn_data->rdh.iType);
815 ok(rgn_data->rdh.nCount >= 1, "Got unexpected count %u.\n", rgn_data->rdh.nCount);
816 ok(EqualRect(&rgn_data->rdh.rcBound, &client_rect),
817 "Got unexpected bounding rect {%d, %d, %d, %d}, expected {%d, %d, %d, %d}.\n",
818 rgn_data->rdh.rcBound.left, rgn_data->rdh.rcBound.top,
819 rgn_data->rdh.rcBound.right, rgn_data->rdh.rcBound.bottom,
820 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
821 HeapFree(GetProcessHeap(), 0, rgn_data);
823 r1 = CreateRectRgn(0, 0, 320, 240);
824 ok(!!r1, "Failed to create region.\n");
825 r2 = CreateRectRgn(320, 240, 640, 480);
826 ok(!!r2, "Failed to create region.\n");
827 CombineRgn(r1, r1, r2, RGN_OR);
828 ret = GetRegionData(r1, 0, NULL);
829 rgn_data = HeapAlloc(GetProcessHeap(), 0, ret);
830 ret = GetRegionData(r1, ret, rgn_data);
831 ok(!!ret, "Failed to get region data.\n");
833 DeleteObject(r2);
834 DeleteObject(r1);
836 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
837 ok(hr == DDERR_CLIPPERISUSINGHWND, "Got unexpected hr %#x.\n", hr);
838 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
839 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
840 hr = IDirectDrawClipper_SetClipList(clipper, rgn_data, 0);
841 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
843 HeapFree(GetProcessHeap(), 0, rgn_data);
845 memset(&surface_desc, 0, sizeof(surface_desc));
846 surface_desc.dwSize = sizeof(surface_desc);
847 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
848 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
849 surface_desc.dwWidth = 640;
850 surface_desc.dwHeight = 480;
851 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
852 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
853 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
854 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
855 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
856 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
858 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src_surface, NULL);
859 ok(SUCCEEDED(hr), "Failed to create source surface, hr %#x.\n", hr);
860 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst_surface, NULL);
861 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
863 memset(&fx, 0, sizeof(fx));
864 fx.dwSize = sizeof(fx);
865 hr = IDirectDrawSurface4_Blt(src_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
866 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
867 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
868 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
870 hr = IDirectDrawSurface4_Lock(src_surface, NULL, &surface_desc, DDLOCK_WAIT, NULL);
871 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
872 ok(U1(surface_desc).lPitch == 2560, "Got unexpected surface pitch %u.\n", U1(surface_desc).lPitch);
873 ptr = surface_desc.lpSurface;
874 memcpy(&ptr[ 0], &src_data[ 0], 6 * sizeof(DWORD));
875 memcpy(&ptr[ 640], &src_data[ 6], 6 * sizeof(DWORD));
876 memcpy(&ptr[1280], &src_data[12], 6 * sizeof(DWORD));
877 hr = IDirectDrawSurface4_Unlock(src_surface, NULL);
878 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
880 hr = IDirectDrawSurface4_SetClipper(dst_surface, clipper);
881 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
883 SetRect(&src_rect, 1, 1, 5, 2);
884 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, src_surface, &src_rect, DDBLT_WAIT, NULL);
885 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
886 for (i = 0; i < 4; ++i)
888 for (j = 0; j < 4; ++j)
890 x = 80 * ((2 * j) + 1);
891 y = 60 * ((2 * i) + 1);
892 color = get_surface_color(dst_surface, x, y);
893 ok(compare_color(color, expected1[i * 4 + j], 1)
894 || broken(compare_color(color, expected1_broken[i * 4 + j], 1)),
895 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected1[i * 4 + j], x, y, color);
899 U5(fx).dwFillColor = 0xff0000ff;
900 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
901 ok(SUCCEEDED(hr), "Failed to clear destination surface, hr %#x.\n", hr);
902 for (i = 0; i < 4; ++i)
904 for (j = 0; j < 4; ++j)
906 x = 80 * ((2 * j) + 1);
907 y = 60 * ((2 * i) + 1);
908 color = get_surface_color(dst_surface, x, y);
909 ok(compare_color(color, expected2[i * 4 + j], 1),
910 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected2[i * 4 + j], x, y, color);
914 hr = IDirectDrawSurface4_BltFast(dst_surface, 0, 0, src_surface, NULL, DDBLTFAST_WAIT);
915 ok(hr == DDERR_BLTFASTCANTCLIP, "Got unexpected hr %#x.\n", hr);
917 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window);
918 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
919 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
920 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
921 DestroyWindow(window);
922 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
923 ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
924 hr = IDirectDrawClipper_SetHWnd(clipper, 0, NULL);
925 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
926 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
927 ok(SUCCEEDED(hr), "Failed to get clip list size, hr %#x.\n", hr);
928 hr = IDirectDrawClipper_SetClipList(clipper, NULL, 0);
929 ok(SUCCEEDED(hr), "Failed to set clip list, hr %#x.\n", hr);
930 hr = IDirectDrawClipper_GetClipList(clipper, NULL, NULL, &ret);
931 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
932 hr = IDirectDrawSurface4_Blt(dst_surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
933 ok(hr == DDERR_NOCLIPLIST, "Got unexpected hr %#x.\n", hr);
935 IDirectDrawSurface4_Release(dst_surface);
936 IDirectDrawSurface4_Release(src_surface);
937 IDirectDrawClipper_Release(clipper);
938 IDirectDraw4_Release(ddraw);
941 static void test_coop_level_d3d_state(void)
943 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
944 IDirectDrawSurface4 *rt, *surface;
945 IDirect3DViewport3 *viewport;
946 IDirect3DDevice3 *device;
947 IDirectDraw4 *ddraw;
948 IDirect3D3 *d3d;
949 D3DCOLOR color;
950 DWORD value;
951 HWND window;
952 HRESULT hr;
954 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
955 0, 0, 640, 480, 0, 0, 0, 0);
956 if (!(device = create_device(window, DDSCL_NORMAL)))
958 skip("Failed to create a 3D device, skipping test.\n");
959 DestroyWindow(window);
960 return;
963 viewport = create_viewport(device, 0, 0, 640, 480);
965 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
966 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
967 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
968 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
969 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
970 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
971 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
972 ok(!value, "Got unexpected alpha blend enable state %#x.\n", value);
973 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
974 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
975 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
976 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
977 color = get_surface_color(rt, 320, 240);
978 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
980 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
981 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
982 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
983 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
984 IDirect3D3_Release(d3d);
985 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
986 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
987 hr = IDirectDrawSurface4_IsLost(rt);
988 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
989 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
990 ok(SUCCEEDED(hr), "Failed to restore surfaces, hr %#x.\n", hr);
991 IDirectDraw4_Release(ddraw);
993 hr = IDirect3DDevice3_GetRenderTarget(device, &surface);
994 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
995 ok(surface == rt, "Got unexpected surface %p.\n", surface);
996 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ZENABLE, &value);
997 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
998 ok(!!value, "Got unexpected z-enable state %#x.\n", value);
999 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, &value);
1000 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1001 ok(!!value, "Got unexpected alpha blend enable state %#x.\n", value);
1002 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);
1003 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1004 color = get_surface_color(rt, 320, 240);
1005 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1007 destroy_viewport(device, viewport);
1008 IDirectDrawSurface4_Release(surface);
1009 IDirectDrawSurface4_Release(rt);
1010 IDirect3DDevice3_Release(device);
1011 DestroyWindow(window);
1014 static void test_surface_interface_mismatch(void)
1016 IDirectDraw4 *ddraw = NULL;
1017 IDirect3D3 *d3d = NULL;
1018 IDirectDrawSurface4 *surface = NULL, *ds;
1019 IDirectDrawSurface3 *surface3 = NULL;
1020 IDirect3DDevice3 *device = NULL;
1021 IDirect3DViewport3 *viewport = NULL;
1022 DDSURFACEDESC2 surface_desc;
1023 DDPIXELFORMAT z_fmt;
1024 ULONG refcount;
1025 HRESULT hr;
1026 D3DCOLOR color;
1027 HWND window;
1028 D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1030 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1031 0, 0, 640, 480, 0, 0, 0, 0);
1032 ddraw = create_ddraw();
1033 ok(!!ddraw, "Failed to create a ddraw object.\n");
1034 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
1035 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1037 memset(&surface_desc, 0, sizeof(surface_desc));
1038 surface_desc.dwSize = sizeof(surface_desc);
1039 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1040 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
1041 surface_desc.dwWidth = 640;
1042 surface_desc.dwHeight = 480;
1044 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1045 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1047 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirectDrawSurface3, (void **)&surface3);
1048 ok(SUCCEEDED(hr), "Failed to QI IDirectDrawSurface3, hr %#x.\n", hr);
1050 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
1052 skip("D3D interface is not available, skipping test.\n");
1053 goto cleanup;
1056 memset(&z_fmt, 0, sizeof(z_fmt));
1057 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
1058 if (FAILED(hr) || !z_fmt.dwSize)
1060 skip("No depth buffer formats available, skipping test.\n");
1061 goto cleanup;
1064 memset(&surface_desc, 0, sizeof(surface_desc));
1065 surface_desc.dwSize = sizeof(surface_desc);
1066 surface_desc.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
1067 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1068 U4(surface_desc).ddpfPixelFormat = z_fmt;
1069 surface_desc.dwWidth = 640;
1070 surface_desc.dwHeight = 480;
1071 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &ds, NULL);
1072 ok(SUCCEEDED(hr), "Failed to create depth buffer, hr %#x.\n", hr);
1073 if (FAILED(hr))
1074 goto cleanup;
1076 /* Using a different surface interface version still works */
1077 hr = IDirectDrawSurface3_AddAttachedSurface(surface3, (IDirectDrawSurface3 *)ds);
1078 ok(SUCCEEDED(hr), "Failed to attach depth buffer, hr %#x.\n", hr);
1079 refcount = IDirectDrawSurface4_Release(ds);
1080 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
1081 if (FAILED(hr))
1082 goto cleanup;
1084 /* Here too */
1085 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, (IDirectDrawSurface4 *)surface3, &device, NULL);
1086 ok(SUCCEEDED(hr), "Failed to create d3d device.\n");
1087 if (FAILED(hr))
1088 goto cleanup;
1090 viewport = create_viewport(device, 0, 0, 640, 480);
1092 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xffff0000, 0.0f, 0);
1093 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1094 color = get_surface_color(surface, 320, 240);
1095 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
1097 cleanup:
1098 if (viewport)
1099 destroy_viewport(device, viewport);
1100 if (surface3) IDirectDrawSurface3_Release(surface3);
1101 if (surface) IDirectDrawSurface4_Release(surface);
1102 if (device) IDirect3DDevice3_Release(device);
1103 if (d3d) IDirect3D3_Release(d3d);
1104 if (ddraw) IDirectDraw4_Release(ddraw);
1105 DestroyWindow(window);
1108 static void test_coop_level_threaded(void)
1110 struct create_window_thread_param p;
1111 IDirectDraw4 *ddraw;
1112 HRESULT hr;
1114 ddraw = create_ddraw();
1115 ok(!!ddraw, "Failed to create a ddraw object.\n");
1116 create_window_thread(&p);
1118 hr = IDirectDraw4_SetCooperativeLevel(ddraw, p.window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
1119 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1121 IDirectDraw4_Release(ddraw);
1122 destroy_window_thread(&p);
1125 static void test_depth_blit(void)
1127 static struct
1129 float x, y, z;
1130 DWORD color;
1132 quad1[] =
1134 { -1.0, 1.0, 0.50f, 0xff00ff00},
1135 { 1.0, 1.0, 0.50f, 0xff00ff00},
1136 { -1.0, -1.0, 0.50f, 0xff00ff00},
1137 { 1.0, -1.0, 0.50f, 0xff00ff00},
1139 static const D3DCOLOR expected_colors[4][4] =
1141 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1142 {0x00ff0000, 0x00ff0000, 0x0000ff00, 0x0000ff00},
1143 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1144 {0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00},
1146 DDSURFACEDESC2 ddsd_new, ddsd_existing;
1148 IDirect3DDevice3 *device;
1149 IDirectDrawSurface4 *ds1, *ds2, *ds3, *rt;
1150 IDirect3DViewport3 *viewport;
1151 RECT src_rect, dst_rect;
1152 unsigned int i, j;
1153 D3DCOLOR color;
1154 HRESULT hr;
1155 IDirect3D3 *d3d;
1156 IDirectDraw4 *ddraw;
1157 DDBLTFX fx;
1158 HWND window;
1159 D3DRECT d3drect;
1161 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1162 0, 0, 640, 480, 0, 0, 0, 0);
1163 if (!(device = create_device(window, DDSCL_NORMAL)))
1165 skip("Failed to create a 3D device, skipping test.\n");
1166 DestroyWindow(window);
1167 return;
1170 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1171 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1172 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1173 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1174 IDirect3D3_Release(d3d);
1176 ds1 = get_depth_stencil(device);
1178 memset(&ddsd_new, 0, sizeof(ddsd_new));
1179 ddsd_new.dwSize = sizeof(ddsd_new);
1180 memset(&ddsd_existing, 0, sizeof(ddsd_existing));
1181 ddsd_existing.dwSize = sizeof(ddsd_existing);
1182 hr = IDirectDrawSurface4_GetSurfaceDesc(ds1, &ddsd_existing);
1183 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
1184 ddsd_new.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
1185 ddsd_new.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
1186 ddsd_new.dwWidth = ddsd_existing.dwWidth;
1187 ddsd_new.dwHeight = ddsd_existing.dwHeight;
1188 U4(ddsd_new).ddpfPixelFormat = U4(ddsd_existing).ddpfPixelFormat;
1189 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds2, NULL);
1190 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1191 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd_new, &ds3, NULL);
1192 ok(SUCCEEDED(hr), "Failed to create a surface, hr %#x.\n", hr);
1193 IDirectDraw4_Release(ddraw);
1195 viewport = create_viewport(device, 0, 0, ddsd_existing.dwWidth, ddsd_existing.dwHeight);
1196 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1197 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
1199 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
1200 ok(SUCCEEDED(hr), "Failed to enable z testing, hr %#x.\n", hr);
1201 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
1202 ok(SUCCEEDED(hr), "Failed to set the z function, hr %#x.\n", hr);
1204 U1(d3drect).x1 = U2(d3drect).y1 = 0;
1205 U3(d3drect).x2 = ddsd_existing.dwWidth; U4(d3drect).y2 = ddsd_existing.dwHeight;
1206 hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER, 0, 0.0f, 0);
1207 ok(SUCCEEDED(hr), "Failed to clear the z buffer, hr %#x.\n", hr);
1209 /* Partial blit. */
1210 SetRect(&src_rect, 0, 0, 320, 240);
1211 SetRect(&dst_rect, 0, 0, 320, 240);
1212 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1213 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1214 /* Different locations. */
1215 SetRect(&src_rect, 0, 0, 320, 240);
1216 SetRect(&dst_rect, 320, 240, 640, 480);
1217 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1218 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1219 /* Streched. */
1220 SetRect(&src_rect, 0, 0, 320, 240);
1221 SetRect(&dst_rect, 0, 0, 640, 480);
1222 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1223 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1224 /* Flipped. */
1225 SetRect(&src_rect, 0, 480, 640, 0);
1226 SetRect(&dst_rect, 0, 0, 640, 480);
1227 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1228 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1229 SetRect(&src_rect, 0, 0, 640, 480);
1230 SetRect(&dst_rect, 0, 480, 640, 0);
1231 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1232 ok(hr == DDERR_INVALIDRECT, "Got unexpected hr %#x.\n", hr);
1233 /* Full, explicit. */
1234 SetRect(&src_rect, 0, 0, 640, 480);
1235 SetRect(&dst_rect, 0, 0, 640, 480);
1236 hr = IDirectDrawSurface4_Blt(ds2, &dst_rect, ds1, &src_rect, DDBLT_WAIT, NULL);
1237 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1238 /* Depth -> color blit: Succeeds on Win7 + Radeon HD 5700, fails on WinXP + Radeon X1600 */
1240 /* Depth blit inside a BeginScene / EndScene pair */
1241 hr = IDirect3DDevice3_BeginScene(device);
1242 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1243 /* From the current depth stencil */
1244 hr = IDirectDrawSurface4_Blt(ds2, NULL, ds1, NULL, DDBLT_WAIT, NULL);
1245 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1246 /* To the current depth stencil */
1247 hr = IDirectDrawSurface4_Blt(ds1, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1248 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1249 /* Between unbound surfaces */
1250 hr = IDirectDrawSurface4_Blt(ds3, NULL, ds2, NULL, DDBLT_WAIT, NULL);
1251 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1252 hr = IDirect3DDevice3_EndScene(device);
1253 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1255 /* Avoid changing the depth stencil, it doesn't work properly on Windows.
1256 * Instead use DDBLT_DEPTHFILL to clear the depth stencil. Unfortunately
1257 * drivers disagree on the meaning of dwFillDepth. Only 0 seems to produce
1258 * a reliable result(z = 0.0) */
1259 memset(&fx, 0, sizeof(fx));
1260 fx.dwSize = sizeof(fx);
1261 hr = IDirectDrawSurface4_Blt(ds2, NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &fx);
1262 ok(SUCCEEDED(hr), "Failed to clear the source z buffer, hr %#x.\n", hr);
1264 hr = IDirect3DViewport3_Clear2(viewport, 1, &d3drect, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, 0xffff0000, 1.0f, 0);
1265 ok(SUCCEEDED(hr), "Failed to clear the color and z buffers, hr %#x.\n", hr);
1266 SetRect(&dst_rect, 0, 0, 320, 240);
1267 hr = IDirectDrawSurface4_Blt(ds1, &dst_rect, ds2, NULL, DDBLT_WAIT, NULL);
1268 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1269 IDirectDrawSurface4_Release(ds3);
1270 IDirectDrawSurface4_Release(ds2);
1271 IDirectDrawSurface4_Release(ds1);
1273 hr = IDirect3DDevice3_BeginScene(device);
1274 ok(SUCCEEDED(hr), "Failed to start a scene, hr %#x.\n", hr);
1275 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZ | D3DFVF_DIFFUSE,
1276 quad1, 4, 0);
1277 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1278 hr = IDirect3DDevice3_EndScene(device);
1279 ok(SUCCEEDED(hr), "Failed to end a scene, hr %#x.\n", hr);
1281 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1282 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1283 for (i = 0; i < 4; ++i)
1285 for (j = 0; j < 4; ++j)
1287 unsigned int x = 80 * ((2 * j) + 1);
1288 unsigned int y = 60 * ((2 * i) + 1);
1289 color = get_surface_color(rt, x, y);
1290 ok(compare_color(color, expected_colors[i][j], 1),
1291 "Expected color 0x%08x at %u,%u, got 0x%08x.\n", expected_colors[i][j], x, y, color);
1294 IDirectDrawSurface4_Release(rt);
1296 destroy_viewport(device, viewport);
1297 IDirect3DDevice3_Release(device);
1298 DestroyWindow(window);
1301 static void test_texture_load_ckey(void)
1303 IDirectDraw4 *ddraw;
1304 IDirectDrawSurface4 *src;
1305 IDirectDrawSurface4 *dst;
1306 IDirect3DTexture2 *src_tex;
1307 IDirect3DTexture2 *dst_tex;
1308 DDSURFACEDESC2 ddsd;
1309 HRESULT hr;
1310 DDCOLORKEY ckey;
1312 ddraw = create_ddraw();
1313 ok(!!ddraw, "Failed to create a ddraw object.\n");
1314 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
1315 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
1317 memset(&ddsd, 0, sizeof(ddsd));
1318 ddsd.dwSize = sizeof(ddsd);
1319 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
1320 ddsd.dwHeight = 128;
1321 ddsd.dwWidth = 128;
1322 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
1323 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &src, NULL);
1324 ok(SUCCEEDED(hr), "Failed to create source texture, hr %#x.\n", hr);
1325 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1326 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &dst, NULL);
1327 ok(SUCCEEDED(hr), "Failed to create destination texture, hr %#x.\n", hr);
1329 hr = IDirectDrawSurface4_QueryInterface(src, &IID_IDirect3DTexture2, (void **)&src_tex);
1330 ok(SUCCEEDED(hr) || hr == E_NOINTERFACE, "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1331 if (FAILED(hr))
1333 /* 64 bit ddraw does not support d3d */
1334 skip("Could not get Direct3DTexture2 interface, skipping texture::Load color keying tests.\n");
1335 IDirectDrawSurface4_Release(dst);
1336 IDirectDrawSurface4_Release(src);
1337 IDirectDraw4_Release(ddraw);
1338 return;
1340 hr = IDirectDrawSurface4_QueryInterface(dst, &IID_IDirect3DTexture2, (void **)&dst_tex);
1341 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x.\n", hr);
1343 /* No surface has a color key */
1344 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1345 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1346 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0xdeadbeef;
1347 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1348 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1349 ok(ckey.dwColorSpaceLowValue == 0xdeadbeef, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1350 ok(ckey.dwColorSpaceHighValue == 0xdeadbeef, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1352 /* Source surface has a color key */
1353 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x0000ff00;
1354 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, &ckey);
1355 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1356 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1357 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1358 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1359 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1360 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1361 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1363 /* Both surfaces have a color key: Dest ckey is overwritten */
1364 ckey.dwColorSpaceLowValue = ckey.dwColorSpaceHighValue = 0x000000ff;
1365 hr = IDirectDrawSurface4_SetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1366 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1367 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1368 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1369 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1370 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1371 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1372 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1374 /* Only the destination has a color key: It is not deleted */
1375 hr = IDirectDrawSurface4_SetColorKey(src, DDCKEY_SRCBLT, NULL);
1376 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1377 hr = IDirectDrawSurface4_GetColorKey(src, DDCKEY_SRCBLT, &ckey);
1378 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1379 hr = IDirect3DTexture2_Load(dst_tex, src_tex);
1380 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1381 hr = IDirectDrawSurface4_GetColorKey(dst, DDCKEY_SRCBLT, &ckey);
1382 ok(SUCCEEDED(hr), "Got unexpected hr %#x.\n", hr);
1383 ok(ckey.dwColorSpaceLowValue == 0x0000ff00, "dwColorSpaceLowValue is %#x.\n", ckey.dwColorSpaceLowValue);
1384 ok(ckey.dwColorSpaceHighValue == 0x0000ff00, "dwColorSpaceHighValue is %#x.\n", ckey.dwColorSpaceHighValue);
1386 IDirect3DTexture2_Release(dst_tex);
1387 IDirect3DTexture2_Release(src_tex);
1388 IDirectDrawSurface4_Release(dst);
1389 IDirectDrawSurface4_Release(src);
1390 IDirectDraw4_Release(ddraw);
1393 static ULONG get_refcount(IUnknown *test_iface)
1395 IUnknown_AddRef(test_iface);
1396 return IUnknown_Release(test_iface);
1399 static void test_viewport(void)
1401 IDirectDraw4 *ddraw;
1402 IDirect3D3 *d3d;
1403 HRESULT hr, old_d3d_ref;
1404 ULONG ref;
1405 IDirect3DViewport *viewport;
1406 IDirect3DViewport2 *viewport2;
1407 IDirect3DViewport3 *viewport3, *another_vp, *test_vp;
1408 IDirectDrawGammaControl *gamma;
1409 IUnknown *unknown;
1410 HWND window;
1411 IDirect3DDevice3 *device;
1413 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1414 0, 0, 640, 480, 0, 0, 0, 0);
1415 if (!(device = create_device(window, DDSCL_NORMAL)))
1417 skip("Failed to create a 3D device, skipping test.\n");
1418 DestroyWindow(window);
1419 return;
1421 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1422 ok(SUCCEEDED(hr), "Failed to get Direct3D3 interface, hr %#x.\n", hr);
1423 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1424 ok(SUCCEEDED(hr), "Failed to get DirectDraw4 interface, hr %#x.\n", hr);
1425 old_d3d_ref = get_refcount((IUnknown *) d3d);
1427 hr = IDirect3D3_CreateViewport(d3d, &viewport3, NULL);
1428 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1429 ref = get_refcount((IUnknown *)viewport3);
1430 ok(ref == 1, "Initial IDirect3DViewport3 refcount is %u\n", ref);
1431 ref = get_refcount((IUnknown *)d3d);
1432 ok(ref == old_d3d_ref, "IDirect3D3 refcount is %u\n", ref);
1434 gamma = (IDirectDrawGammaControl *)0xdeadbeef;
1435 hr = IDirect3DViewport2_QueryInterface(viewport3, &IID_IDirectDrawGammaControl, (void **)&gamma);
1436 ok(hr == E_NOINTERFACE, "Got unexpected hr %#x.\n", hr);
1437 ok(gamma == NULL, "Interface not set to NULL by failed QI call: %p\n", gamma);
1438 if (SUCCEEDED(hr)) IDirectDrawGammaControl_Release(gamma);
1439 /* NULL iid: Segfaults */
1441 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport, (void **)&viewport);
1442 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport, hr %#x.\n", hr);
1443 if (viewport)
1445 ref = get_refcount((IUnknown *)viewport);
1446 ok(ref == 2, "IDirect3DViewport refcount is %u\n", ref);
1447 ref = get_refcount((IUnknown *)viewport3);
1448 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1449 IDirect3DViewport_Release(viewport);
1450 viewport = NULL;
1453 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IDirect3DViewport3, (void **)&viewport2);
1454 ok(SUCCEEDED(hr), "Failed to QI IDirect3DViewport3, hr %#x.\n", hr);
1455 if (viewport2)
1457 ref = get_refcount((IUnknown *)viewport2);
1458 ok(ref == 2, "IDirect3DViewport2 refcount is %u\n", ref);
1459 ref = get_refcount((IUnknown *)viewport3);
1460 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1461 IDirect3DViewport3_Release(viewport2);
1464 hr = IDirect3DViewport3_QueryInterface(viewport3, &IID_IUnknown, (void **)&unknown);
1465 ok(SUCCEEDED(hr), "Failed to QI IUnknown, hr %#x.\n", hr);
1466 if (unknown)
1468 ref = get_refcount((IUnknown *)viewport3);
1469 ok(ref == 2, "IDirect3DViewport3 refcount is %u\n", ref);
1470 ref = get_refcount(unknown);
1471 ok(ref == 2, "IUnknown refcount is %u\n", ref);
1472 IUnknown_Release(unknown);
1475 /* AddViewport(NULL): Segfault */
1476 hr = IDirect3DDevice3_DeleteViewport(device, NULL);
1477 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1478 hr = IDirect3DDevice3_GetCurrentViewport(device, NULL);
1479 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1481 hr = IDirect3D3_CreateViewport(d3d, &another_vp, NULL);
1482 ok(SUCCEEDED(hr), "Failed to create viewport, hr %#x.\n", hr);
1484 /* Setting a viewport not in the viewport list fails */
1485 hr = IDirect3DDevice3_SetCurrentViewport(device, another_vp);
1486 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
1488 hr = IDirect3DDevice3_AddViewport(device, viewport3);
1489 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1490 ref = get_refcount((IUnknown *) viewport3);
1491 ok(ref == 2, "viewport3 refcount is %d\n", ref);
1492 hr = IDirect3DDevice3_AddViewport(device, another_vp);
1493 ok(SUCCEEDED(hr), "Failed to add viewport to device, hr %#x.\n", hr);
1494 ref = get_refcount((IUnknown *) another_vp);
1495 ok(ref == 2, "another_vp refcount is %d\n", ref);
1497 test_vp = (IDirect3DViewport3 *) 0xbaadc0de;
1498 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1499 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1500 ok(test_vp == (IDirect3DViewport3 *) 0xbaadc0de, "Got unexpected pointer %p\n", test_vp);
1502 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport3);
1503 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1504 ref = get_refcount((IUnknown *) viewport3);
1505 ok(ref == 3, "viewport3 refcount is %d\n", ref);
1506 ref = get_refcount((IUnknown *) device);
1507 ok(ref == 1, "device refcount is %d\n", ref);
1509 test_vp = NULL;
1510 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1511 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1512 ok(test_vp == viewport3, "Got unexpected viewport %p\n", test_vp);
1513 ref = get_refcount((IUnknown *) viewport3);
1514 ok(ref == 4, "viewport3 refcount is %d\n", ref);
1515 if(test_vp) IDirect3DViewport3_Release(test_vp);
1517 /* GetCurrentViewport with a viewport set and NULL input param: Segfault */
1519 /* Cannot set the viewport to NULL */
1520 hr = IDirect3DDevice3_SetCurrentViewport(device, NULL);
1521 ok(hr == DDERR_INVALIDPARAMS, "Failed to set viewport to NULL, hr %#x.\n", hr);
1522 test_vp = NULL;
1523 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1524 ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1525 ok(test_vp == viewport3, "Got unexpected viewport %p\n", test_vp);
1526 if(test_vp) IDirect3DViewport3_Release(test_vp);
1528 /* SetCurrentViewport properly releases the old viewport's reference */
1529 hr = IDirect3DDevice3_SetCurrentViewport(device, another_vp);
1530 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1531 ref = get_refcount((IUnknown *) viewport3);
1532 ok(ref == 2, "viewport3 refcount is %d\n", ref);
1533 ref = get_refcount((IUnknown *) another_vp);
1534 ok(ref == 3, "another_vp refcount is %d\n", ref);
1536 /* Unlike device2::DeleteViewport, device3::DeleteViewport releases the
1537 * reference held by SetCurrentViewport */
1538 hr = IDirect3DDevice3_DeleteViewport(device, another_vp);
1539 ok(SUCCEEDED(hr), "Failed to delete viewport from device, hr %#x.\n", hr);
1540 ref = get_refcount((IUnknown *) another_vp);
1541 ok(ref == 1, "another_vp refcount is %d\n", ref);
1543 /* GetCurrentViewport still fails */
1544 test_vp = NULL;
1545 hr = IDirect3DDevice3_GetCurrentViewport(device, &test_vp);
1546 ok(hr == D3DERR_NOCURRENTVIEWPORT, "Got unexpected hr %#x.\n", hr);
1547 ok(test_vp == NULL, "Got unexpected viewport %p\n", test_vp);
1548 if(test_vp) IDirect3DViewport3_Release(test_vp);
1550 /* Setting a different viewport doesn't have any surprises now */
1551 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport3);
1552 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1553 ref = get_refcount((IUnknown *) viewport3);
1554 ok(ref == 3, "viewport3 refcount is %d\n", ref);
1555 ref = get_refcount((IUnknown *) another_vp);
1556 ok(ref == 1, "another_vp refcount is %d\n", ref);
1558 /* Destroying the device removes the viewport and releases the reference */
1559 IDirect3DDevice3_Release(device);
1560 ref = get_refcount((IUnknown *) viewport3);
1561 ok(ref == 1, "viewport3 refcount is %d\n", ref);
1563 ref = IDirect3DViewport3_Release(another_vp);
1564 ok(ref == 0, "Got unexpected ref %d\n", ref);
1565 ref = IDirect3DViewport3_Release(viewport3);
1566 ok(ref == 0, "Got unexpected ref %d\n", ref);
1567 IDirect3D3_Release(d3d);
1568 DestroyWindow(window);
1569 IDirectDraw4_Release(ddraw);
1572 static void test_zenable(void)
1574 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1575 static struct
1577 struct vec4 position;
1578 D3DCOLOR diffuse;
1580 tquad[] =
1582 {{ 0.0f, 480.0f, -0.5f, 1.0f}, 0xff00ff00},
1583 {{ 0.0f, 0.0f, -0.5f, 1.0f}, 0xff00ff00},
1584 {{640.0f, 480.0f, 1.5f, 1.0f}, 0xff00ff00},
1585 {{640.0f, 0.0f, 1.5f, 1.0f}, 0xff00ff00},
1587 IDirect3DViewport3 *viewport;
1588 IDirect3DDevice3 *device;
1589 IDirectDrawSurface4 *rt;
1590 D3DCOLOR color;
1591 HWND window;
1592 HRESULT hr;
1593 UINT x, y;
1594 UINT i, j;
1596 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1597 0, 0, 640, 480, 0, 0, 0, 0);
1598 if (!(device = create_device(window, DDSCL_NORMAL)))
1600 skip("Failed to create a 3D device, skipping test.\n");
1601 DestroyWindow(window);
1602 return;
1605 viewport = create_viewport(device, 0, 0, 640, 480);
1606 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1607 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1609 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
1610 ok(SUCCEEDED(hr), "Failed to disable z-buffering, hr %#x.\n", hr);
1612 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 0.0f, 0);
1613 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1614 hr = IDirect3DDevice3_BeginScene(device);
1615 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1616 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, tquad, 4, 0);
1617 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1618 hr = IDirect3DDevice3_EndScene(device);
1619 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1621 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1622 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1623 for (i = 0; i < 4; ++i)
1625 for (j = 0; j < 4; ++j)
1627 x = 80 * ((2 * j) + 1);
1628 y = 60 * ((2 * i) + 1);
1629 color = get_surface_color(rt, x, y);
1630 ok(compare_color(color, 0x0000ff00, 1),
1631 "Expected color 0x0000ff00 at %u, %u, got 0x%08x.\n", x, y, color);
1634 IDirectDrawSurface4_Release(rt);
1636 destroy_viewport(device, viewport);
1637 IDirect3DDevice3_Release(device);
1638 DestroyWindow(window);
1641 static void test_ck_rgba(void)
1643 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1644 static struct
1646 struct vec4 position;
1647 struct vec2 texcoord;
1649 tquad[] =
1651 {{ 0.0f, 480.0f, 0.25f, 1.0f}, {0.0f, 0.0f}},
1652 {{ 0.0f, 0.0f, 0.25f, 1.0f}, {0.0f, 1.0f}},
1653 {{640.0f, 480.0f, 0.25f, 1.0f}, {1.0f, 0.0f}},
1654 {{640.0f, 0.0f, 0.25f, 1.0f}, {1.0f, 1.0f}},
1655 {{ 0.0f, 480.0f, 0.75f, 1.0f}, {0.0f, 0.0f}},
1656 {{ 0.0f, 0.0f, 0.75f, 1.0f}, {0.0f, 1.0f}},
1657 {{640.0f, 480.0f, 0.75f, 1.0f}, {1.0f, 0.0f}},
1658 {{640.0f, 0.0f, 0.75f, 1.0f}, {1.0f, 1.0f}},
1660 static const struct
1662 D3DCOLOR fill_color;
1663 BOOL color_key;
1664 BOOL blend;
1665 D3DCOLOR result1;
1666 D3DCOLOR result2;
1668 tests[] =
1670 {0xff00ff00, TRUE, TRUE, 0x00ff0000, 0x000000ff},
1671 {0xff00ff00, TRUE, FALSE, 0x00ff0000, 0x000000ff},
1672 {0xff00ff00, FALSE, TRUE, 0x0000ff00, 0x0000ff00},
1673 {0xff00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1674 {0x7f00ff00, TRUE, TRUE, 0x00807f00, 0x00807f00},
1675 {0x7f00ff00, TRUE, FALSE, 0x0000ff00, 0x0000ff00},
1676 {0x7f00ff00, FALSE, TRUE, 0x00807f00, 0x00807f00},
1677 {0x7f00ff00, FALSE, FALSE, 0x0000ff00, 0x0000ff00},
1680 IDirectDrawSurface4 *surface;
1681 IDirect3DViewport3 *viewport;
1682 DDSURFACEDESC2 surface_desc;
1683 IDirect3DTexture2 *texture;
1684 IDirect3DDevice3 *device;
1685 IDirectDrawSurface4 *rt;
1686 IDirectDraw4 *ddraw;
1687 IDirect3D3 *d3d;
1688 D3DCOLOR color;
1689 HWND window;
1690 DDBLTFX fx;
1691 HRESULT hr;
1692 UINT i;
1694 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1695 0, 0, 640, 480, 0, 0, 0, 0);
1696 if (!(device = create_device(window, DDSCL_NORMAL)))
1698 skip("Failed to create a 3D device, skipping test.\n");
1699 DestroyWindow(window);
1700 return;
1703 viewport = create_viewport(device, 0, 0, 640, 480);
1704 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1705 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1707 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1708 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1709 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1710 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1711 IDirect3D3_Release(d3d);
1713 memset(&surface_desc, 0, sizeof(surface_desc));
1714 surface_desc.dwSize = sizeof(surface_desc);
1715 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1716 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1717 surface_desc.dwWidth = 256;
1718 surface_desc.dwHeight = 256;
1719 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1720 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
1721 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1722 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1723 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1724 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1725 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
1726 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0xff00ff00;
1727 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0xff00ff00;
1728 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1729 ok(SUCCEEDED(hr), "Failed to create destination surface, hr %#x.\n", hr);
1730 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1731 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1733 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1734 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1735 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
1736 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1737 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
1738 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1740 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1741 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1743 for (i = 0; i < sizeof(tests) / sizeof(*tests); ++i)
1745 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, tests[i].color_key);
1746 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1747 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ALPHABLENDENABLE, tests[i].blend);
1748 ok(SUCCEEDED(hr), "Failed to enable alpha blending, hr %#x.\n", hr);
1750 memset(&fx, 0, sizeof(fx));
1751 fx.dwSize = sizeof(fx);
1752 U5(fx).dwFillColor = tests[i].fill_color;
1753 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1754 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1756 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
1757 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffff0000, 1.0f, 0);
1758 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1759 hr = IDirect3DDevice3_BeginScene(device);
1760 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1761 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1762 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1763 hr = IDirect3DDevice3_EndScene(device);
1764 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1766 color = get_surface_color(rt, 320, 240);
1767 if (i == 2)
1768 todo_wine ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1769 tests[i].result1, i, color);
1770 else
1771 ok(compare_color(color, tests[i].result1, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1772 tests[i].result1, i, color);
1774 U5(fx).dwFillColor = 0xff0000ff;
1775 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1776 ok(SUCCEEDED(hr), "Failed to fill texture, hr %#x.\n", hr);
1778 hr = IDirect3DDevice3_BeginScene(device);
1779 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1780 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[4], 4, 0);
1781 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1782 hr = IDirect3DDevice3_EndScene(device);
1783 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1785 /* This tests that fragments that are masked out by the color key are
1786 * discarded, instead of just fully transparent. */
1787 color = get_surface_color(rt, 320, 240);
1788 if (i == 2)
1789 todo_wine ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1790 tests[i].result2, i, color);
1791 else
1792 ok(compare_color(color, tests[i].result2, 1), "Expected color 0x%08x for test %u, got 0x%08x.\n",
1793 tests[i].result2, i, color);
1796 IDirectDrawSurface4_Release(rt);
1797 IDirect3DTexture2_Release(texture);
1798 IDirectDrawSurface4_Release(surface);
1799 destroy_viewport(device, viewport);
1800 IDirectDraw4_Release(ddraw);
1801 IDirect3DDevice3_Release(device);
1802 DestroyWindow(window);
1805 static void test_ck_default(void)
1807 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
1808 static struct
1810 struct vec4 position;
1811 struct vec2 texcoord;
1813 tquad[] =
1815 {{ 0.0f, 480.0f, 0.0f, 1.0f}, {0.0f, 0.0f}},
1816 {{ 0.0f, 0.0f, 0.0f, 1.0f}, {0.0f, 1.0f}},
1817 {{640.0f, 480.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
1818 {{640.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
1820 IDirectDrawSurface4 *surface, *rt;
1821 IDirect3DViewport3 *viewport;
1822 DDSURFACEDESC2 surface_desc;
1823 IDirect3DTexture2 *texture;
1824 IDirect3DDevice3 *device;
1825 IDirectDraw4 *ddraw;
1826 IDirect3D3 *d3d;
1827 D3DCOLOR color;
1828 DWORD value;
1829 HWND window;
1830 DDBLTFX fx;
1831 HRESULT hr;
1833 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1834 0, 0, 640, 480, 0, 0, 0, 0);
1836 if (!(device = create_device(window, DDSCL_NORMAL)))
1838 skip("Failed to create a 3D device, skipping test.\n");
1839 DestroyWindow(window);
1840 return;
1843 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1844 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1845 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1846 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1847 IDirect3D3_Release(d3d);
1849 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
1850 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
1852 viewport = create_viewport(device, 0, 0, 640, 480);
1853 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
1854 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
1856 memset(&surface_desc, 0, sizeof(surface_desc));
1857 surface_desc.dwSize = sizeof(surface_desc);
1858 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CKSRCBLT;
1859 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1860 surface_desc.dwWidth = 256;
1861 surface_desc.dwHeight = 256;
1862 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
1863 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
1864 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
1865 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
1866 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
1867 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
1868 surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue = 0x000000ff;
1869 surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue = 0x000000ff;
1870 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1871 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1872 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
1873 ok(SUCCEEDED(hr), "Failed to get texture interface, hr %#x.\n", hr);
1874 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
1875 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x.\n", hr);
1877 memset(&fx, 0, sizeof(fx));
1878 fx.dwSize = sizeof(fx);
1879 U5(fx).dwFillColor = 0x000000ff;
1880 hr = IDirectDrawSurface4_Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
1881 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
1883 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1884 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1885 hr = IDirect3DDevice3_BeginScene(device);
1886 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1887 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1888 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1889 ok(!value, "Got unexpected color keying state %#x.\n", value);
1890 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1891 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1892 hr = IDirect3DDevice3_EndScene(device);
1893 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1894 color = get_surface_color(rt, 320, 240);
1895 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
1897 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
1898 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
1899 hr = IDirect3DDevice3_BeginScene(device);
1900 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
1901 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, TRUE);
1902 ok(SUCCEEDED(hr), "Failed to enable color keying, hr %#x.\n", hr);
1903 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, D3DFVF_XYZRHW | D3DFVF_TEX1, &tquad[0], 4, 0);
1904 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
1905 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_COLORKEYENABLE, &value);
1906 ok(SUCCEEDED(hr), "Failed to get render state, hr %#x.\n", hr);
1907 ok(!!value, "Got unexpected color keying state %#x.\n", value);
1908 hr = IDirect3DDevice3_EndScene(device);
1909 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
1910 color = get_surface_color(rt, 320, 240);
1911 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
1913 IDirect3DTexture_Release(texture);
1914 IDirectDrawSurface4_Release(surface);
1915 destroy_viewport(device, viewport);
1916 IDirectDrawSurface4_Release(rt);
1917 IDirect3DDevice3_Release(device);
1918 IDirectDraw4_Release(ddraw);
1919 DestroyWindow(window);
1922 static void test_ck_complex(void)
1924 IDirectDrawSurface4 *surface, *mipmap, *tmp;
1925 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
1926 DDSURFACEDESC2 surface_desc;
1927 IDirect3DDevice3 *device;
1928 DDCOLORKEY color_key;
1929 IDirectDraw4 *ddraw;
1930 IDirect3D3 *d3d;
1931 unsigned int i;
1932 ULONG refcount;
1933 HWND window;
1934 HRESULT hr;
1936 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
1937 0, 0, 640, 480, 0, 0, 0, 0);
1938 if (!(device = create_device(window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
1940 skip("Failed to create a 3D device, skipping test.\n");
1941 DestroyWindow(window);
1942 return;
1944 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
1945 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
1946 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
1947 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
1948 IDirect3D3_Release(d3d);
1950 memset(&surface_desc, 0, sizeof(surface_desc));
1951 surface_desc.dwSize = sizeof(surface_desc);
1952 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
1953 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
1954 surface_desc.dwWidth = 128;
1955 surface_desc.dwHeight = 128;
1956 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
1957 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
1959 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1960 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
1961 color_key.dwColorSpaceLowValue = 0x0000ff00;
1962 color_key.dwColorSpaceHighValue = 0x0000ff00;
1963 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1964 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
1965 memset(&color_key, 0, sizeof(color_key));
1966 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
1967 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
1968 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1969 color_key.dwColorSpaceLowValue);
1970 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
1971 color_key.dwColorSpaceHighValue);
1973 mipmap = surface;
1974 IDirectDrawSurface_AddRef(mipmap);
1975 for (i = 0; i < 7; ++i)
1977 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
1978 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
1980 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1981 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
1982 color_key.dwColorSpaceLowValue = 0x000000ff;
1983 color_key.dwColorSpaceHighValue = 0x000000ff;
1984 hr = IDirectDrawSurface4_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1985 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x, i %u.\n", hr, i);
1986 memset(&color_key, 0, sizeof(color_key));
1987 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
1988 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x, i %u.\n", hr, i);
1989 ok(color_key.dwColorSpaceLowValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1990 color_key.dwColorSpaceLowValue, i);
1991 ok(color_key.dwColorSpaceHighValue == 0x000000ff, "Got unexpected value 0x%08x, i %u.\n",
1992 color_key.dwColorSpaceHighValue, i);
1994 IDirectDrawSurface_Release(mipmap);
1995 mipmap = tmp;
1998 memset(&color_key, 0, sizeof(color_key));
1999 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2000 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2001 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2002 color_key.dwColorSpaceLowValue);
2003 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2004 color_key.dwColorSpaceHighValue);
2006 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
2007 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
2008 IDirectDrawSurface_Release(mipmap);
2009 refcount = IDirectDrawSurface4_Release(surface);
2010 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2012 memset(&surface_desc, 0, sizeof(surface_desc));
2013 surface_desc.dwSize = sizeof(surface_desc);
2014 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
2015 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
2016 U5(surface_desc).dwBackBufferCount = 1;
2017 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2018 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2020 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2021 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x.\n", hr);
2022 color_key.dwColorSpaceLowValue = 0x0000ff00;
2023 color_key.dwColorSpaceHighValue = 0x0000ff00;
2024 hr = IDirectDrawSurface4_SetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2025 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2026 memset(&color_key, 0, sizeof(color_key));
2027 hr = IDirectDrawSurface4_GetColorKey(surface, DDCKEY_SRCBLT, &color_key);
2028 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2029 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2030 color_key.dwColorSpaceLowValue);
2031 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2032 color_key.dwColorSpaceHighValue);
2034 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &tmp);
2035 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
2037 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2038 ok(hr == DDERR_NOCOLORKEY, "Got unexpected hr %#x, i %u.\n", hr, i);
2039 color_key.dwColorSpaceLowValue = 0x0000ff00;
2040 color_key.dwColorSpaceHighValue = 0x0000ff00;
2041 hr = IDirectDrawSurface4_SetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2042 ok(SUCCEEDED(hr), "Failed to set color key, hr %#x.\n", hr);
2043 memset(&color_key, 0, sizeof(color_key));
2044 hr = IDirectDrawSurface4_GetColorKey(tmp, DDCKEY_SRCBLT, &color_key);
2045 ok(SUCCEEDED(hr), "Failed to get color key, hr %#x.\n", hr);
2046 ok(color_key.dwColorSpaceLowValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2047 color_key.dwColorSpaceLowValue);
2048 ok(color_key.dwColorSpaceHighValue == 0x0000ff00, "Got unexpected value 0x%08x.\n",
2049 color_key.dwColorSpaceHighValue);
2051 IDirectDrawSurface_Release(tmp);
2053 refcount = IDirectDrawSurface4_Release(surface);
2054 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2055 IDirectDraw4_Release(ddraw);
2056 refcount = IDirect3DDevice3_Release(device);
2057 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
2058 DestroyWindow(window);
2061 struct qi_test
2063 REFIID iid;
2064 REFIID refcount_iid;
2065 HRESULT hr;
2068 static void test_qi(const char *test_name, IUnknown *base_iface,
2069 REFIID refcount_iid, const struct qi_test *tests, UINT entry_count)
2071 ULONG refcount, expected_refcount;
2072 IUnknown *iface1, *iface2;
2073 HRESULT hr;
2074 UINT i, j;
2076 for (i = 0; i < entry_count; ++i)
2078 hr = IUnknown_QueryInterface(base_iface, tests[i].iid, (void **)&iface1);
2079 ok(hr == tests[i].hr, "Got hr %#x for test \"%s\" %u.\n", hr, test_name, i);
2080 if (SUCCEEDED(hr))
2082 for (j = 0; j < entry_count; ++j)
2084 hr = IUnknown_QueryInterface(iface1, tests[j].iid, (void **)&iface2);
2085 ok(hr == tests[j].hr, "Got hr %#x for test \"%s\" %u, %u.\n", hr, test_name, i, j);
2086 if (SUCCEEDED(hr))
2088 expected_refcount = 0;
2089 if (IsEqualGUID(refcount_iid, tests[j].refcount_iid))
2090 ++expected_refcount;
2091 if (IsEqualGUID(tests[i].refcount_iid, tests[j].refcount_iid))
2092 ++expected_refcount;
2093 refcount = IUnknown_Release(iface2);
2094 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, %u, expected %u.\n",
2095 refcount, test_name, i, j, expected_refcount);
2099 expected_refcount = 0;
2100 if (IsEqualGUID(refcount_iid, tests[i].refcount_iid))
2101 ++expected_refcount;
2102 refcount = IUnknown_Release(iface1);
2103 ok(refcount == expected_refcount, "Got refcount %u for test \"%s\" %u, expected %u.\n",
2104 refcount, test_name, i, expected_refcount);
2109 static void test_surface_qi(void)
2111 static const struct qi_test tests[] =
2113 {&IID_IDirect3DTexture2, &IID_IDirectDrawSurface4, S_OK },
2114 {&IID_IDirect3DTexture, &IID_IDirectDrawSurface4, S_OK },
2115 {&IID_IDirectDrawGammaControl, &IID_IDirectDrawGammaControl, S_OK },
2116 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2117 {&IID_IDirectDrawSurface7, &IID_IDirectDrawSurface7, S_OK },
2118 {&IID_IDirectDrawSurface4, &IID_IDirectDrawSurface4, S_OK },
2119 {&IID_IDirectDrawSurface3, &IID_IDirectDrawSurface3, S_OK },
2120 {&IID_IDirectDrawSurface2, &IID_IDirectDrawSurface2, S_OK },
2121 {&IID_IDirectDrawSurface, &IID_IDirectDrawSurface, S_OK },
2122 {&IID_IDirect3DDevice7, NULL, E_INVALIDARG },
2123 {&IID_IDirect3DDevice3, NULL, E_INVALIDARG },
2124 {&IID_IDirect3DDevice2, NULL, E_INVALIDARG },
2125 {&IID_IDirect3DDevice, NULL, E_INVALIDARG },
2126 {&IID_IDirect3D7, NULL, E_INVALIDARG },
2127 {&IID_IDirect3D3, NULL, E_INVALIDARG },
2128 {&IID_IDirect3D2, NULL, E_INVALIDARG },
2129 {&IID_IDirect3D, NULL, E_INVALIDARG },
2130 {&IID_IDirectDraw7, NULL, E_INVALIDARG },
2131 {&IID_IDirectDraw4, NULL, E_INVALIDARG },
2132 {&IID_IDirectDraw3, NULL, E_INVALIDARG },
2133 {&IID_IDirectDraw2, NULL, E_INVALIDARG },
2134 {&IID_IDirectDraw, NULL, E_INVALIDARG },
2135 {&IID_IDirect3DLight, NULL, E_INVALIDARG },
2136 {&IID_IDirect3DMaterial, NULL, E_INVALIDARG },
2137 {&IID_IDirect3DMaterial2, NULL, E_INVALIDARG },
2138 {&IID_IDirect3DMaterial3, NULL, E_INVALIDARG },
2139 {&IID_IDirect3DExecuteBuffer, NULL, E_INVALIDARG },
2140 {&IID_IDirect3DViewport, NULL, E_INVALIDARG },
2141 {&IID_IDirect3DViewport2, NULL, E_INVALIDARG },
2142 {&IID_IDirect3DViewport3, NULL, E_INVALIDARG },
2143 {&IID_IDirect3DVertexBuffer, NULL, E_INVALIDARG },
2144 {&IID_IDirect3DVertexBuffer7, NULL, E_INVALIDARG },
2145 {&IID_IDirectDrawPalette, NULL, E_INVALIDARG },
2146 {&IID_IDirectDrawClipper, NULL, E_INVALIDARG },
2147 {&IID_IUnknown, &IID_IDirectDrawSurface, S_OK },
2150 IDirectDrawSurface4 *surface;
2151 DDSURFACEDESC2 surface_desc;
2152 IDirect3DDevice3 *device;
2153 IDirectDraw4 *ddraw;
2154 HWND window;
2155 HRESULT hr;
2157 if (!GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx"))
2159 win_skip("DirectDrawCreateEx not available, skipping test.\n");
2160 return;
2163 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2164 0, 0, 640, 480, 0, 0, 0, 0);
2165 /* Try to create a D3D device to see if the ddraw implementation supports
2166 * D3D. 64-bit ddraw in particular doesn't seem to support D3D, and
2167 * doesn't support e.g. the IDirect3DTexture interfaces. */
2168 if (!(device = create_device(window, DDSCL_NORMAL)))
2170 skip("Failed to create a 3D device, skipping test.\n");
2171 DestroyWindow(window);
2172 return;
2174 IDirect3DDevice_Release(device);
2175 ddraw = create_ddraw();
2176 ok(!!ddraw, "Failed to create a ddraw object.\n");
2177 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2178 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
2180 memset(&surface_desc, 0, sizeof(surface_desc));
2181 surface_desc.dwSize = sizeof(surface_desc);
2182 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
2183 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
2184 surface_desc.dwWidth = 512;
2185 surface_desc.dwHeight = 512;
2186 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
2187 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
2189 test_qi("surface_qi", (IUnknown *)surface, &IID_IDirectDrawSurface4, tests, sizeof(tests) / sizeof(*tests));
2191 IDirectDrawSurface4_Release(surface);
2192 IDirectDraw4_Release(ddraw);
2193 DestroyWindow(window);
2196 static void test_device_qi(void)
2198 static const struct qi_test tests[] =
2200 {&IID_IDirect3DTexture2, NULL, E_NOINTERFACE},
2201 {&IID_IDirect3DTexture, NULL, E_NOINTERFACE},
2202 {&IID_IDirectDrawGammaControl, NULL, E_NOINTERFACE},
2203 {&IID_IDirectDrawColorControl, NULL, E_NOINTERFACE},
2204 {&IID_IDirectDrawSurface7, NULL, E_NOINTERFACE},
2205 {&IID_IDirectDrawSurface4, NULL, E_NOINTERFACE},
2206 {&IID_IDirectDrawSurface3, NULL, E_NOINTERFACE},
2207 {&IID_IDirectDrawSurface2, NULL, E_NOINTERFACE},
2208 {&IID_IDirectDrawSurface, NULL, E_NOINTERFACE},
2209 {&IID_IDirect3DDevice7, NULL, E_NOINTERFACE},
2210 {&IID_IDirect3DDevice3, &IID_IDirect3DDevice3, S_OK },
2211 {&IID_IDirect3DDevice2, &IID_IDirect3DDevice3, S_OK },
2212 {&IID_IDirect3DDevice, &IID_IDirect3DDevice3, S_OK },
2213 {&IID_IDirect3DRampDevice, NULL, E_NOINTERFACE},
2214 {&IID_IDirect3DRGBDevice, NULL, E_NOINTERFACE},
2215 {&IID_IDirect3DHALDevice, NULL, E_NOINTERFACE},
2216 {&IID_IDirect3DMMXDevice, NULL, E_NOINTERFACE},
2217 {&IID_IDirect3DRefDevice, NULL, E_NOINTERFACE},
2218 {&IID_IDirect3DTnLHalDevice, NULL, E_NOINTERFACE},
2219 {&IID_IDirect3DNullDevice, NULL, E_NOINTERFACE},
2220 {&IID_IDirect3D7, NULL, E_NOINTERFACE},
2221 {&IID_IDirect3D3, NULL, E_NOINTERFACE},
2222 {&IID_IDirect3D2, NULL, E_NOINTERFACE},
2223 {&IID_IDirect3D, NULL, E_NOINTERFACE},
2224 {&IID_IDirectDraw7, NULL, E_NOINTERFACE},
2225 {&IID_IDirectDraw4, NULL, E_NOINTERFACE},
2226 {&IID_IDirectDraw3, NULL, E_NOINTERFACE},
2227 {&IID_IDirectDraw2, NULL, E_NOINTERFACE},
2228 {&IID_IDirectDraw, NULL, E_NOINTERFACE},
2229 {&IID_IDirect3DLight, NULL, E_NOINTERFACE},
2230 {&IID_IDirect3DMaterial, NULL, E_NOINTERFACE},
2231 {&IID_IDirect3DMaterial2, NULL, E_NOINTERFACE},
2232 {&IID_IDirect3DMaterial3, NULL, E_NOINTERFACE},
2233 {&IID_IDirect3DExecuteBuffer, NULL, E_NOINTERFACE},
2234 {&IID_IDirect3DViewport, NULL, E_NOINTERFACE},
2235 {&IID_IDirect3DViewport2, NULL, E_NOINTERFACE},
2236 {&IID_IDirect3DViewport3, NULL, E_NOINTERFACE},
2237 {&IID_IDirect3DVertexBuffer, NULL, E_NOINTERFACE},
2238 {&IID_IDirect3DVertexBuffer7, NULL, E_NOINTERFACE},
2239 {&IID_IDirectDrawPalette, NULL, E_NOINTERFACE},
2240 {&IID_IDirectDrawClipper, NULL, E_NOINTERFACE},
2241 {&IID_IUnknown, &IID_IDirect3DDevice3, S_OK },
2244 IDirect3DDevice3 *device;
2245 HWND window;
2247 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2248 0, 0, 640, 480, 0, 0, 0, 0);
2249 if (!(device = create_device(window, DDSCL_NORMAL)))
2251 skip("Failed to create a 3D device, skipping test.\n");
2252 DestroyWindow(window);
2253 return;
2256 test_qi("device_qi", (IUnknown *)device, &IID_IDirect3DDevice3, tests, sizeof(tests) / sizeof(*tests));
2258 IDirect3DDevice3_Release(device);
2259 DestroyWindow(window);
2262 static void test_wndproc(void)
2264 LONG_PTR proc, ddraw_proc;
2265 IDirectDraw4 *ddraw;
2266 WNDCLASSA wc = {0};
2267 HWND window;
2268 HRESULT hr;
2269 ULONG ref;
2271 static struct message messages[] =
2273 {WM_WINDOWPOSCHANGING, FALSE, 0},
2274 {WM_MOVE, FALSE, 0},
2275 {WM_SIZE, FALSE, 0},
2276 {WM_WINDOWPOSCHANGING, FALSE, 0},
2277 {WM_ACTIVATE, FALSE, 0},
2278 {WM_SETFOCUS, FALSE, 0},
2279 {0, FALSE, 0},
2282 /* DDSCL_EXCLUSIVE replaces the window's window proc. */
2283 ddraw = create_ddraw();
2284 ok(!!ddraw, "Failed to create a ddraw object.\n");
2286 wc.lpfnWndProc = test_proc;
2287 wc.lpszClassName = "ddraw_test_wndproc_wc";
2288 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2290 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
2291 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
2293 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2294 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2295 (LONG_PTR)test_proc, proc);
2296 expect_messages = messages;
2297 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2298 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2299 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2300 expect_messages = NULL;
2301 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2302 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2303 (LONG_PTR)test_proc, proc);
2304 ref = IDirectDraw4_Release(ddraw);
2305 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2306 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2307 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2308 (LONG_PTR)test_proc, proc);
2310 /* DDSCL_NORMAL doesn't. */
2311 ddraw = create_ddraw();
2312 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2313 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2314 (LONG_PTR)test_proc, proc);
2315 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
2316 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2317 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2318 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2319 (LONG_PTR)test_proc, proc);
2320 ref = IDirectDraw4_Release(ddraw);
2321 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2322 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2323 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2324 (LONG_PTR)test_proc, proc);
2326 /* The original window proc is only restored by ddraw if the current
2327 * window proc matches the one ddraw set. This also affects switching
2328 * from DDSCL_NORMAL to DDSCL_EXCLUSIVE. */
2329 ddraw = create_ddraw();
2330 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2331 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2332 (LONG_PTR)test_proc, proc);
2333 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2334 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2335 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2336 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2337 (LONG_PTR)test_proc, proc);
2338 ddraw_proc = proc;
2339 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2340 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2341 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2342 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2343 (LONG_PTR)test_proc, proc);
2344 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2345 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2346 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2347 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2348 (LONG_PTR)test_proc, proc);
2349 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2350 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2351 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2352 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2353 (LONG_PTR)DefWindowProcA, proc);
2354 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2355 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2356 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)ddraw_proc);
2357 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2358 (LONG_PTR)DefWindowProcA, proc);
2359 ref = IDirectDraw4_Release(ddraw);
2360 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2361 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2362 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2363 (LONG_PTR)test_proc, proc);
2365 ddraw = create_ddraw();
2366 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2367 ok(proc == (LONG_PTR)test_proc, "Expected wndproc %#lx, got %#lx.\n",
2368 (LONG_PTR)test_proc, proc);
2369 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2370 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2371 proc = SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)DefWindowProcA);
2372 ok(proc != (LONG_PTR)test_proc, "Expected wndproc != %#lx, got %#lx.\n",
2373 (LONG_PTR)test_proc, proc);
2374 ref = IDirectDraw4_Release(ddraw);
2375 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2376 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
2377 ok(proc == (LONG_PTR)DefWindowProcA, "Expected wndproc %#lx, got %#lx.\n",
2378 (LONG_PTR)DefWindowProcA, proc);
2380 fix_wndproc(window, (LONG_PTR)test_proc);
2381 expect_messages = NULL;
2382 DestroyWindow(window);
2383 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
2386 static void test_window_style(void)
2388 LONG style, exstyle, tmp, expected_style;
2389 RECT fullscreen_rect, r;
2390 IDirectDraw4 *ddraw;
2391 HWND window;
2392 HRESULT hr;
2393 ULONG ref;
2394 BOOL ret;
2396 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2397 0, 0, 100, 100, 0, 0, 0, 0);
2398 ddraw = create_ddraw();
2399 ok(!!ddraw, "Failed to create a ddraw object.\n");
2401 style = GetWindowLongA(window, GWL_STYLE);
2402 exstyle = GetWindowLongA(window, GWL_EXSTYLE);
2403 SetRect(&fullscreen_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2405 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2406 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2408 tmp = GetWindowLongA(window, GWL_STYLE);
2409 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2410 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2411 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2413 GetWindowRect(window, &r);
2414 ok(EqualRect(&r, &fullscreen_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2415 fullscreen_rect.left, fullscreen_rect.top, fullscreen_rect.right, fullscreen_rect.bottom,
2416 r.left, r.top, r.right, r.bottom);
2417 GetClientRect(window, &r);
2418 todo_wine ok(!EqualRect(&r, &fullscreen_rect), "Client rect and window rect are equal.\n");
2420 ret = SetForegroundWindow(GetDesktopWindow());
2421 ok(ret, "Failed to set foreground window.\n");
2423 tmp = GetWindowLongA(window, GWL_STYLE);
2424 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2425 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2426 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2428 ret = SetForegroundWindow(window);
2429 ok(ret, "Failed to set foreground window.\n");
2430 /* Windows 7 (but not Vista and XP) show the window when it receives focus. Hide it again,
2431 * the next tests expect this. */
2432 ShowWindow(window, SW_HIDE);
2434 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2435 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2437 tmp = GetWindowLongA(window, GWL_STYLE);
2438 todo_wine ok(tmp == style, "Expected window style %#x, got %#x.\n", style, tmp);
2439 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2440 todo_wine ok(tmp == exstyle, "Expected window extended style %#x, got %#x.\n", exstyle, tmp);
2442 ShowWindow(window, SW_SHOW);
2443 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2444 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2446 tmp = GetWindowLongA(window, GWL_STYLE);
2447 expected_style = style | WS_VISIBLE;
2448 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2449 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2450 expected_style = exstyle | WS_EX_TOPMOST;
2451 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2453 ret = SetForegroundWindow(GetDesktopWindow());
2454 ok(ret, "Failed to set foreground window.\n");
2455 tmp = GetWindowLongA(window, GWL_STYLE);
2456 expected_style = style | WS_VISIBLE | WS_MINIMIZE;
2457 todo_wine ok(tmp == expected_style, "Expected window style %#x, got %#x.\n", expected_style, tmp);
2458 tmp = GetWindowLongA(window, GWL_EXSTYLE);
2459 expected_style = exstyle | WS_EX_TOPMOST;
2460 todo_wine ok(tmp == expected_style, "Expected window extended style %#x, got %#x.\n", expected_style, tmp);
2462 ref = IDirectDraw4_Release(ddraw);
2463 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2465 DestroyWindow(window);
2468 static void test_redundant_mode_set(void)
2470 DDSURFACEDESC2 surface_desc = {0};
2471 IDirectDraw4 *ddraw;
2472 HWND window;
2473 HRESULT hr;
2474 RECT r, s;
2475 ULONG ref;
2477 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
2478 0, 0, 100, 100, 0, 0, 0, 0);
2479 ddraw = create_ddraw();
2480 ok(!!ddraw, "Failed to create a ddraw object.\n");
2482 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2483 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2485 surface_desc.dwSize = sizeof(surface_desc);
2486 hr = IDirectDraw4_GetDisplayMode(ddraw, &surface_desc);
2487 ok(SUCCEEDED(hr), "GetDipslayMode failed, hr %#x.\n", hr);
2489 hr = IDirectDraw4_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2490 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2491 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2493 GetWindowRect(window, &r);
2494 r.right /= 2;
2495 r.bottom /= 2;
2496 SetWindowPos(window, HWND_TOP, r.left, r.top, r.right, r.bottom, 0);
2497 GetWindowRect(window, &s);
2498 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2499 r.left, r.top, r.right, r.bottom,
2500 s.left, s.top, s.right, s.bottom);
2502 hr = IDirectDraw4_SetDisplayMode(ddraw, surface_desc.dwWidth, surface_desc.dwHeight,
2503 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount, 0, 0);
2504 ok(SUCCEEDED(hr), "SetDisplayMode failed, hr %#x.\n", hr);
2506 GetWindowRect(window, &s);
2507 ok(EqualRect(&r, &s), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2508 r.left, r.top, r.right, r.bottom,
2509 s.left, s.top, s.right, s.bottom);
2511 ref = IDirectDraw4_Release(ddraw);
2512 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2514 DestroyWindow(window);
2517 static SIZE screen_size, screen_size2;
2519 static LRESULT CALLBACK mode_set_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2521 if (message == WM_SIZE)
2523 screen_size.cx = GetSystemMetrics(SM_CXSCREEN);
2524 screen_size.cy = GetSystemMetrics(SM_CYSCREEN);
2527 return test_proc(hwnd, message, wparam, lparam);
2530 static LRESULT CALLBACK mode_set_proc2(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
2532 if (message == WM_SIZE)
2534 screen_size2.cx = GetSystemMetrics(SM_CXSCREEN);
2535 screen_size2.cy = GetSystemMetrics(SM_CYSCREEN);
2538 return test_proc(hwnd, message, wparam, lparam);
2541 struct test_coop_level_mode_set_enum_param
2543 DWORD ddraw_width, ddraw_height, user32_width, user32_height;
2546 static HRESULT CALLBACK test_coop_level_mode_set_enum_cb(DDSURFACEDESC2 *surface_desc, void *context)
2548 struct test_coop_level_mode_set_enum_param *param = context;
2550 if (U1(U4(*surface_desc).ddpfPixelFormat).dwRGBBitCount != registry_mode.dmBitsPerPel)
2551 return DDENUMRET_OK;
2552 if (surface_desc->dwWidth == registry_mode.dmPelsWidth
2553 && surface_desc->dwHeight == registry_mode.dmPelsHeight)
2554 return DDENUMRET_OK;
2556 if (!param->ddraw_width)
2558 param->ddraw_width = surface_desc->dwWidth;
2559 param->ddraw_height = surface_desc->dwHeight;
2560 return DDENUMRET_OK;
2562 if (surface_desc->dwWidth == param->ddraw_width && surface_desc->dwHeight == param->ddraw_height)
2563 return DDENUMRET_OK;
2565 param->user32_width = surface_desc->dwWidth;
2566 param->user32_height = surface_desc->dwHeight;
2567 return DDENUMRET_CANCEL;
2570 static void test_coop_level_mode_set(void)
2572 IDirectDrawSurface4 *primary;
2573 RECT registry_rect, ddraw_rect, user32_rect, r;
2574 IDirectDraw4 *ddraw;
2575 DDSURFACEDESC2 ddsd;
2576 WNDCLASSA wc = {0};
2577 HWND window, window2;
2578 HRESULT hr;
2579 ULONG ref;
2580 MSG msg;
2581 struct test_coop_level_mode_set_enum_param param;
2582 DEVMODEW devmode;
2583 BOOL ret;
2584 LONG change_ret;
2586 static const struct message exclusive_messages[] =
2588 {WM_WINDOWPOSCHANGING, FALSE, 0},
2589 {WM_WINDOWPOSCHANGED, FALSE, 0},
2590 {WM_SIZE, FALSE, 0},
2591 {WM_DISPLAYCHANGE, FALSE, 0},
2592 {0, FALSE, 0},
2594 static const struct message exclusive_focus_loss_messages[] =
2596 {WM_ACTIVATE, TRUE, WA_INACTIVE},
2597 {WM_DISPLAYCHANGE, FALSE, 0},
2598 {WM_WINDOWPOSCHANGING, FALSE, 0},
2599 /* Like d3d8 and d3d9 ddraw seems to use SW_SHOWMINIMIZED instead of
2600 * SW_MINIMIZED, causing a recursive window activation that does not
2601 * produe the same result in Wine yet. Ignore the difference for now.
2602 * {WM_ACTIVATE, TRUE, 0x200000 | WA_ACTIVE}, */
2603 {WM_WINDOWPOSCHANGED, FALSE, 0},
2604 {WM_MOVE, FALSE, 0},
2605 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2606 {WM_ACTIVATEAPP, TRUE, FALSE},
2607 {0, FALSE, 0},
2609 static const struct message exclusive_focus_restore_messages[] =
2611 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* From the ShowWindow(SW_RESTORE). */
2612 {WM_WINDOWPOSCHANGING, FALSE, 0}, /* Generated by ddraw, matches d3d9 behavior. */
2613 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching previous message. */
2614 {WM_SIZE, FALSE, 0}, /* DefWindowProc. */
2615 {WM_DISPLAYCHANGE, FALSE, 0}, /* Ddraw restores mode. */
2616 /* Native redundantly sets the window size here. */
2617 {WM_ACTIVATEAPP, TRUE, TRUE}, /* End of ddraw's hooks. */
2618 {WM_WINDOWPOSCHANGED, FALSE, 0}, /* Matching the one from ShowWindow. */
2619 {WM_MOVE, FALSE, 0}, /* DefWindowProc. */
2620 {WM_SIZE, TRUE, SIZE_RESTORED}, /* DefWindowProc. */
2621 {0, FALSE, 0},
2623 static const struct message sc_restore_messages[] =
2625 {WM_SYSCOMMAND, TRUE, SC_RESTORE},
2626 {WM_WINDOWPOSCHANGING, FALSE, 0},
2627 {WM_WINDOWPOSCHANGED, FALSE, 0},
2628 {WM_SIZE, TRUE, SIZE_RESTORED},
2629 {0, FALSE, 0},
2631 static const struct message sc_minimize_messages[] =
2633 {WM_SYSCOMMAND, TRUE, SC_MINIMIZE},
2634 {WM_WINDOWPOSCHANGING, FALSE, 0},
2635 {WM_WINDOWPOSCHANGED, FALSE, 0},
2636 {WM_SIZE, TRUE, SIZE_MINIMIZED},
2637 {0, FALSE, 0},
2639 static const struct message sc_maximize_messages[] =
2641 {WM_SYSCOMMAND, TRUE, SC_MAXIMIZE},
2642 {WM_WINDOWPOSCHANGING, FALSE, 0},
2643 {WM_WINDOWPOSCHANGED, FALSE, 0},
2644 {WM_SIZE, TRUE, SIZE_MAXIMIZED},
2645 {0, FALSE, 0},
2648 static const struct message normal_messages[] =
2650 {WM_DISPLAYCHANGE, FALSE, 0},
2651 {0, FALSE, 0},
2654 ddraw = create_ddraw();
2655 ok(!!ddraw, "Failed to create a ddraw object.\n");
2657 memset(&param, 0, sizeof(param));
2658 hr = IDirectDraw4_EnumDisplayModes(ddraw, 0, NULL, &param, test_coop_level_mode_set_enum_cb);
2659 ok(SUCCEEDED(hr), "Failed to enumerate display mode, hr %#x.\n", hr);
2660 ref = IDirectDraw4_Release(ddraw);
2661 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
2663 if (!param.user32_height)
2665 skip("Fewer than 3 different modes supported, skipping mode restore test.\n");
2666 return;
2669 SetRect(&registry_rect, 0, 0, registry_mode.dmPelsWidth, registry_mode.dmPelsHeight);
2670 SetRect(&ddraw_rect, 0, 0, param.ddraw_width, param.ddraw_height);
2671 SetRect(&user32_rect, 0, 0, param.user32_width, param.user32_height);
2673 memset(&devmode, 0, sizeof(devmode));
2674 devmode.dmSize = sizeof(devmode);
2675 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2676 devmode.dmPelsWidth = param.user32_width;
2677 devmode.dmPelsHeight = param.user32_height;
2678 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2679 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2681 ddraw = create_ddraw();
2682 ok(!!ddraw, "Failed to create a ddraw object.\n");
2684 wc.lpfnWndProc = mode_set_proc;
2685 wc.lpszClassName = "ddraw_test_wndproc_wc";
2686 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2687 wc.lpfnWndProc = mode_set_proc2;
2688 wc.lpszClassName = "ddraw_test_wndproc_wc2";
2689 ok(RegisterClassA(&wc), "Failed to register window class.\n");
2691 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test", WS_OVERLAPPEDWINDOW,
2692 0, 0, 100, 100, 0, 0, 0, 0);
2693 window2 = CreateWindowA("ddraw_test_wndproc_wc2", "ddraw_test", WS_OVERLAPPEDWINDOW,
2694 0, 0, 100, 100, 0, 0, 0, 0);
2696 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2697 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2699 GetWindowRect(window, &r);
2700 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2701 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2702 r.left, r.top, r.right, r.bottom);
2704 memset(&ddsd, 0, sizeof(ddsd));
2705 ddsd.dwSize = sizeof(ddsd);
2706 ddsd.dwFlags = DDSD_CAPS;
2707 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2709 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2710 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2711 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2712 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2713 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2714 param.user32_width, ddsd.dwWidth);
2715 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2716 param.user32_height, ddsd.dwHeight);
2718 GetWindowRect(window, &r);
2719 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2720 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2721 r.left, r.top, r.right, r.bottom);
2723 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2724 expect_messages = exclusive_messages;
2725 screen_size.cx = 0;
2726 screen_size.cy = 0;
2728 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2729 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2731 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2732 expect_messages = NULL;
2733 ok(screen_size.cx == param.ddraw_width && screen_size.cy == param.ddraw_height,
2734 "Expected screen size %ux%u, got %ux%u.\n",
2735 param.ddraw_width, param.ddraw_height, screen_size.cx, screen_size.cy);
2737 GetWindowRect(window, &r);
2738 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2739 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2740 r.left, r.top, r.right, r.bottom);
2742 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2743 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2744 ok(ddsd.dwWidth == param.user32_width, "Expected surface width %u, got %u.\n",
2745 param.user32_width, ddsd.dwWidth);
2746 ok(ddsd.dwHeight == param.user32_height, "Expected surface height %u, got %u.\n",
2747 param.user32_height, ddsd.dwHeight);
2748 IDirectDrawSurface4_Release(primary);
2750 memset(&ddsd, 0, sizeof(ddsd));
2751 ddsd.dwSize = sizeof(ddsd);
2752 ddsd.dwFlags = DDSD_CAPS;
2753 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2755 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2756 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2757 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2758 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2759 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2760 param.ddraw_width, ddsd.dwWidth);
2761 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2762 param.ddraw_height, ddsd.dwHeight);
2764 GetWindowRect(window, &r);
2765 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2766 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2767 r.left, r.top, r.right, r.bottom);
2769 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2770 expect_messages = exclusive_messages;
2771 screen_size.cx = 0;
2772 screen_size.cy = 0;
2774 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2775 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2777 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2778 expect_messages = NULL;
2779 ok(screen_size.cx == param.user32_width && screen_size.cy == param.user32_height,
2780 "Expected screen size %ux%u, got %ux%u.\n",
2781 param.user32_width, param.user32_height, screen_size.cx, screen_size.cy);
2783 GetWindowRect(window, &r);
2784 ok(EqualRect(&r, &user32_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2785 user32_rect.left, user32_rect.top, user32_rect.right, user32_rect.bottom,
2786 r.left, r.top, r.right, r.bottom);
2788 expect_messages = exclusive_focus_loss_messages;
2789 ret = SetForegroundWindow(GetDesktopWindow());
2790 ok(ret, "Failed to set foreground window.\n");
2791 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2792 memset(&devmode, 0, sizeof(devmode));
2793 devmode.dmSize = sizeof(devmode);
2794 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2795 ok(ret, "Failed to get display mode.\n");
2796 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
2797 && devmode.dmPelsHeight == registry_mode.dmPelsHeight, "Got unexpect screen size %ux%u.\n",
2798 devmode.dmPelsWidth, devmode.dmPelsHeight);
2800 expect_messages = exclusive_focus_restore_messages;
2801 ShowWindow(window, SW_RESTORE);
2802 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2804 GetWindowRect(window, &r);
2805 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2806 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
2807 r.left, r.top, r.right, r.bottom);
2808 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
2809 ok(ret, "Failed to get display mode.\n");
2810 ok(devmode.dmPelsWidth == param.ddraw_width
2811 && devmode.dmPelsHeight == param.ddraw_height, "Got unexpect screen size %ux%u.\n",
2812 devmode.dmPelsWidth, devmode.dmPelsHeight);
2814 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2815 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2816 /* Normally the primary should be restored here. Unfortunately this causes the
2817 * GetSurfaceDesc call after the next display mode change to crash on the Windows 8
2818 * testbot. Another Restore call would presumably avoid the crash, but it also moots
2819 * the point of the GetSurfaceDesc call. */
2821 expect_messages = sc_minimize_messages;
2822 SendMessageA(window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
2823 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2824 expect_messages = NULL;
2826 expect_messages = sc_restore_messages;
2827 SendMessageA(window, WM_SYSCOMMAND, SC_RESTORE, 0);
2828 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2829 expect_messages = NULL;
2831 expect_messages = sc_maximize_messages;
2832 SendMessageA(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
2833 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2834 expect_messages = NULL;
2836 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
2837 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2839 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2840 expect_messages = exclusive_messages;
2841 screen_size.cx = 0;
2842 screen_size.cy = 0;
2844 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
2845 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2847 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2848 expect_messages = NULL;
2849 ok(screen_size.cx == registry_mode.dmPelsWidth
2850 && screen_size.cy == registry_mode.dmPelsHeight,
2851 "Expected screen size %ux%u, got %ux%u.\n",
2852 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size.cx, screen_size.cy);
2854 GetWindowRect(window, &r);
2855 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2856 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2857 r.left, r.top, r.right, r.bottom);
2859 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2860 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2861 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2862 param.ddraw_width, ddsd.dwWidth);
2863 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2864 param.ddraw_height, ddsd.dwHeight);
2865 IDirectDrawSurface4_Release(primary);
2867 /* For Wine. */
2868 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
2869 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2871 memset(&ddsd, 0, sizeof(ddsd));
2872 ddsd.dwSize = sizeof(ddsd);
2873 ddsd.dwFlags = DDSD_CAPS;
2874 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2876 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2877 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2878 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2879 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2880 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2881 registry_mode.dmPelsWidth, ddsd.dwWidth);
2882 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2883 registry_mode.dmPelsHeight, ddsd.dwHeight);
2885 GetWindowRect(window, &r);
2886 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2887 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2888 r.left, r.top, r.right, r.bottom);
2890 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
2891 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
2893 GetWindowRect(window, &r);
2894 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2895 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2896 r.left, r.top, r.right, r.bottom);
2898 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2899 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2900 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2901 registry_mode.dmPelsWidth, ddsd.dwWidth);
2902 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2903 registry_mode.dmPelsHeight, ddsd.dwHeight);
2904 IDirectDrawSurface4_Release(primary);
2906 memset(&ddsd, 0, sizeof(ddsd));
2907 ddsd.dwSize = sizeof(ddsd);
2908 ddsd.dwFlags = DDSD_CAPS;
2909 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2911 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2912 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2913 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2914 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2915 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2916 registry_mode.dmPelsWidth, ddsd.dwWidth);
2917 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2918 registry_mode.dmPelsHeight, ddsd.dwHeight);
2920 GetWindowRect(window, &r);
2921 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2922 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2923 r.left, r.top, r.right, r.bottom);
2925 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2926 expect_messages = normal_messages;
2927 screen_size.cx = 0;
2928 screen_size.cy = 0;
2930 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
2931 devmode.dmPelsWidth = param.user32_width;
2932 devmode.dmPelsHeight = param.user32_height;
2933 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
2934 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
2936 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2937 expect_messages = NULL;
2938 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2940 GetWindowRect(window, &r);
2941 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2942 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2943 r.left, r.top, r.right, r.bottom);
2945 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2946 expect_messages = normal_messages;
2947 screen_size.cx = 0;
2948 screen_size.cy = 0;
2950 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
2951 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
2953 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2954 expect_messages = NULL;
2955 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
2957 GetWindowRect(window, &r);
2958 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2959 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2960 r.left, r.top, r.right, r.bottom);
2962 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2963 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2964 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
2965 registry_mode.dmPelsWidth, ddsd.dwWidth);
2966 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
2967 registry_mode.dmPelsHeight, ddsd.dwHeight);
2968 IDirectDrawSurface4_Release(primary);
2970 memset(&ddsd, 0, sizeof(ddsd));
2971 ddsd.dwSize = sizeof(ddsd);
2972 ddsd.dwFlags = DDSD_CAPS;
2973 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
2975 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
2976 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
2977 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
2978 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
2979 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
2980 param.ddraw_width, ddsd.dwWidth);
2981 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
2982 param.ddraw_height, ddsd.dwHeight);
2984 GetWindowRect(window, &r);
2985 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
2986 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
2987 r.left, r.top, r.right, r.bottom);
2989 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
2990 expect_messages = normal_messages;
2991 screen_size.cx = 0;
2992 screen_size.cy = 0;
2994 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
2995 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
2997 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
2998 expect_messages = NULL;
2999 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3001 GetWindowRect(window, &r);
3002 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3003 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3004 r.left, r.top, r.right, r.bottom);
3006 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3007 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3008 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3009 param.ddraw_width, ddsd.dwWidth);
3010 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3011 param.ddraw_height, ddsd.dwHeight);
3012 IDirectDrawSurface4_Release(primary);
3014 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3015 ok(ret, "Failed to get display mode.\n");
3016 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3017 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3018 "Expected resolution %ux%u, got %ux%u.\n",
3019 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3020 devmode.dmPelsWidth, devmode.dmPelsHeight);
3021 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3022 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3024 memset(&ddsd, 0, sizeof(ddsd));
3025 ddsd.dwSize = sizeof(ddsd);
3026 ddsd.dwFlags = DDSD_CAPS;
3027 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3029 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3030 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3031 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3032 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3033 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3034 registry_mode.dmPelsWidth, ddsd.dwWidth);
3035 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3036 registry_mode.dmPelsHeight, ddsd.dwHeight);
3038 GetWindowRect(window, &r);
3039 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3040 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3041 r.left, r.top, r.right, r.bottom);
3043 /* DDSCL_NORMAL | DDSCL_FULLSCREEN behaves the same as just DDSCL_NORMAL.
3044 * Resizing the window on mode changes is a property of DDSCL_EXCLUSIVE,
3045 * not DDSCL_FULLSCREEN. */
3046 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3047 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3049 GetWindowRect(window, &r);
3050 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3051 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3052 r.left, r.top, r.right, r.bottom);
3054 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3055 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3056 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3057 registry_mode.dmPelsWidth, ddsd.dwWidth);
3058 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3059 registry_mode.dmPelsHeight, ddsd.dwHeight);
3060 IDirectDrawSurface4_Release(primary);
3062 memset(&ddsd, 0, sizeof(ddsd));
3063 ddsd.dwSize = sizeof(ddsd);
3064 ddsd.dwFlags = DDSD_CAPS;
3065 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3067 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3068 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3069 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3070 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3071 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3072 registry_mode.dmPelsWidth, ddsd.dwWidth);
3073 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3074 registry_mode.dmPelsHeight, ddsd.dwHeight);
3076 GetWindowRect(window, &r);
3077 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3078 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3079 r.left, r.top, r.right, r.bottom);
3081 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3082 expect_messages = normal_messages;
3083 screen_size.cx = 0;
3084 screen_size.cy = 0;
3086 devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
3087 devmode.dmPelsWidth = param.user32_width;
3088 devmode.dmPelsHeight = param.user32_height;
3089 change_ret = ChangeDisplaySettingsW(&devmode, CDS_FULLSCREEN);
3090 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3092 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3093 expect_messages = NULL;
3094 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3096 GetWindowRect(window, &r);
3097 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3098 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3099 r.left, r.top, r.right, r.bottom);
3101 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3102 expect_messages = normal_messages;
3103 screen_size.cx = 0;
3104 screen_size.cy = 0;
3106 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3107 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3109 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3110 expect_messages = NULL;
3111 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3113 GetWindowRect(window, &r);
3114 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3115 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3116 r.left, r.top, r.right, r.bottom);
3118 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3119 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3120 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3121 registry_mode.dmPelsWidth, ddsd.dwWidth);
3122 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3123 registry_mode.dmPelsHeight, ddsd.dwHeight);
3124 IDirectDrawSurface4_Release(primary);
3126 memset(&ddsd, 0, sizeof(ddsd));
3127 ddsd.dwSize = sizeof(ddsd);
3128 ddsd.dwFlags = DDSD_CAPS;
3129 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3131 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3132 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3133 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3134 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3135 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3136 param.ddraw_width, ddsd.dwWidth);
3137 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3138 param.ddraw_height, ddsd.dwHeight);
3140 GetWindowRect(window, &r);
3141 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3142 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3143 r.left, r.top, r.right, r.bottom);
3145 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3146 expect_messages = normal_messages;
3147 screen_size.cx = 0;
3148 screen_size.cy = 0;
3150 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3151 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3153 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3154 expect_messages = NULL;
3155 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n", screen_size.cx, screen_size.cy);
3157 GetWindowRect(window, &r);
3158 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3159 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3160 r.left, r.top, r.right, r.bottom);
3162 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3163 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3164 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3165 param.ddraw_width, ddsd.dwWidth);
3166 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3167 param.ddraw_height, ddsd.dwHeight);
3168 IDirectDrawSurface4_Release(primary);
3170 ret = EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &devmode);
3171 ok(ret, "Failed to get display mode.\n");
3172 ok(devmode.dmPelsWidth == registry_mode.dmPelsWidth
3173 && devmode.dmPelsHeight == registry_mode.dmPelsHeight,
3174 "Expected resolution %ux%u, got %ux%u.\n",
3175 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3176 devmode.dmPelsWidth, devmode.dmPelsHeight);
3177 change_ret = ChangeDisplaySettingsW(NULL, CDS_FULLSCREEN);
3178 ok(change_ret == DISP_CHANGE_SUCCESSFUL, "Failed to change display mode, ret %#x.\n", change_ret);
3180 memset(&ddsd, 0, sizeof(ddsd));
3181 ddsd.dwSize = sizeof(ddsd);
3182 ddsd.dwFlags = DDSD_CAPS;
3183 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3185 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3186 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3187 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3188 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3189 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3190 registry_mode.dmPelsWidth, ddsd.dwWidth);
3191 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3192 registry_mode.dmPelsHeight, ddsd.dwHeight);
3193 IDirectDrawSurface4_Release(primary);
3195 GetWindowRect(window, &r);
3196 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3197 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3198 r.left, r.top, r.right, r.bottom);
3200 /* Changing the coop level from EXCLUSIVE to NORMAL restores the screen resolution */
3201 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3202 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3203 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3204 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3206 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3207 expect_messages = exclusive_messages;
3208 screen_size.cx = 0;
3209 screen_size.cy = 0;
3211 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3212 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3214 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3215 expect_messages = NULL;
3216 ok(screen_size.cx == registry_mode.dmPelsWidth
3217 && screen_size.cy == registry_mode.dmPelsHeight,
3218 "Expected screen size %ux%u, got %ux%u.\n",
3219 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight,
3220 screen_size.cx, screen_size.cy);
3222 GetWindowRect(window, &r);
3223 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3224 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3225 r.left, r.top, r.right, r.bottom);
3227 memset(&ddsd, 0, sizeof(ddsd));
3228 ddsd.dwSize = sizeof(ddsd);
3229 ddsd.dwFlags = DDSD_CAPS;
3230 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3232 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3233 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3234 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3235 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3236 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3237 registry_mode.dmPelsWidth, ddsd.dwWidth);
3238 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3239 registry_mode.dmPelsHeight, ddsd.dwHeight);
3240 IDirectDrawSurface4_Release(primary);
3242 /* The screen restore is a property of DDSCL_EXCLUSIVE */
3243 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL | DDSCL_FULLSCREEN);
3244 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3245 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3246 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3248 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3249 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3251 memset(&ddsd, 0, sizeof(ddsd));
3252 ddsd.dwSize = sizeof(ddsd);
3253 ddsd.dwFlags = DDSD_CAPS;
3254 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3256 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3257 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3258 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3259 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3260 ok(ddsd.dwWidth == param.ddraw_width, "Expected surface width %u, got %u.\n",
3261 param.ddraw_width, ddsd.dwWidth);
3262 ok(ddsd.dwHeight == param.ddraw_height, "Expected surface height %u, got %u.\n",
3263 param.ddraw_height, ddsd.dwHeight);
3264 IDirectDrawSurface4_Release(primary);
3266 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3267 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3269 /* If the window is changed at the same time, messages are sent to the new window. */
3270 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3271 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3272 hr = set_display_mode(ddraw, param.ddraw_width, param.ddraw_height);
3273 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3275 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
3276 expect_messages = exclusive_messages;
3277 screen_size.cx = 0;
3278 screen_size.cy = 0;
3279 screen_size2.cx = 0;
3280 screen_size2.cy = 0;
3282 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3283 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3285 ok(!expect_messages->message, "Expected message %#x, but didn't receive it.\n", expect_messages->message);
3286 expect_messages = NULL;
3287 ok(!screen_size.cx && !screen_size.cy, "Got unexpected screen size %ux%u.\n",
3288 screen_size.cx, screen_size.cy);
3289 ok(screen_size2.cx == registry_mode.dmPelsWidth && screen_size2.cy == registry_mode.dmPelsHeight,
3290 "Expected screen size 2 %ux%u, got %ux%u.\n",
3291 registry_mode.dmPelsWidth, registry_mode.dmPelsHeight, screen_size2.cx, screen_size2.cy);
3293 GetWindowRect(window, &r);
3294 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3295 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
3296 r.left, r.top, r.right, r.bottom);
3297 GetWindowRect(window2, &r);
3298 ok(EqualRect(&r, &registry_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3299 registry_rect.left, registry_rect.top, registry_rect.right, registry_rect.bottom,
3300 r.left, r.top, r.right, r.bottom);
3302 memset(&ddsd, 0, sizeof(ddsd));
3303 ddsd.dwSize = sizeof(ddsd);
3304 ddsd.dwFlags = DDSD_CAPS;
3305 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3307 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
3308 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
3309 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &ddsd);
3310 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
3311 ok(ddsd.dwWidth == registry_mode.dmPelsWidth, "Expected surface width %u, got %u.\n",
3312 registry_mode.dmPelsWidth, ddsd.dwWidth);
3313 ok(ddsd.dwHeight == registry_mode.dmPelsHeight, "Expected surface height %u, got %u.\n",
3314 registry_mode.dmPelsHeight, ddsd.dwHeight);
3315 IDirectDrawSurface4_Release(primary);
3317 ref = IDirectDraw4_Release(ddraw);
3318 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3320 GetWindowRect(window, &r);
3321 ok(EqualRect(&r, &ddraw_rect), "Expected {%d, %d, %d, %d}, got {%d, %d, %d, %d}.\n",
3322 ddraw_rect.left, ddraw_rect.top, ddraw_rect.right, ddraw_rect.bottom,
3323 r.left, r.top, r.right, r.bottom);
3325 expect_messages = NULL;
3326 DestroyWindow(window);
3327 DestroyWindow(window2);
3328 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
3329 UnregisterClassA("ddraw_test_wndproc_wc2", GetModuleHandleA(NULL));
3332 static void test_coop_level_mode_set_multi(void)
3334 IDirectDraw4 *ddraw1, *ddraw2;
3335 UINT w, h;
3336 HWND window;
3337 HRESULT hr;
3338 ULONG ref;
3340 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3341 0, 0, 100, 100, 0, 0, 0, 0);
3342 ddraw1 = create_ddraw();
3343 ok(!!ddraw1, "Failed to create a ddraw object.\n");
3345 /* With just a single ddraw object, the display mode is restored on
3346 * release. */
3347 hr = set_display_mode(ddraw1, 800, 600);
3348 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3349 w = GetSystemMetrics(SM_CXSCREEN);
3350 ok(w == 800, "Got unexpected screen width %u.\n", w);
3351 h = GetSystemMetrics(SM_CYSCREEN);
3352 ok(h == 600, "Got unexpected screen height %u.\n", h);
3354 ref = IDirectDraw4_Release(ddraw1);
3355 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3356 w = GetSystemMetrics(SM_CXSCREEN);
3357 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3358 h = GetSystemMetrics(SM_CYSCREEN);
3359 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3361 /* When there are multiple ddraw objects, the display mode is restored to
3362 * the initial mode, before the first SetDisplayMode() call. */
3363 ddraw1 = create_ddraw();
3364 hr = set_display_mode(ddraw1, 800, 600);
3365 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3366 w = GetSystemMetrics(SM_CXSCREEN);
3367 ok(w == 800, "Got unexpected screen width %u.\n", w);
3368 h = GetSystemMetrics(SM_CYSCREEN);
3369 ok(h == 600, "Got unexpected screen height %u.\n", h);
3371 ddraw2 = create_ddraw();
3372 hr = set_display_mode(ddraw2, 640, 480);
3373 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3374 w = GetSystemMetrics(SM_CXSCREEN);
3375 ok(w == 640, "Got unexpected screen width %u.\n", w);
3376 h = GetSystemMetrics(SM_CYSCREEN);
3377 ok(h == 480, "Got unexpected screen height %u.\n", h);
3379 ref = IDirectDraw4_Release(ddraw2);
3380 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3381 w = GetSystemMetrics(SM_CXSCREEN);
3382 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3383 h = GetSystemMetrics(SM_CYSCREEN);
3384 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3386 ref = IDirectDraw4_Release(ddraw1);
3387 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3388 w = GetSystemMetrics(SM_CXSCREEN);
3389 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3390 h = GetSystemMetrics(SM_CYSCREEN);
3391 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3393 /* Regardless of release ordering. */
3394 ddraw1 = create_ddraw();
3395 hr = set_display_mode(ddraw1, 800, 600);
3396 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3397 w = GetSystemMetrics(SM_CXSCREEN);
3398 ok(w == 800, "Got unexpected screen width %u.\n", w);
3399 h = GetSystemMetrics(SM_CYSCREEN);
3400 ok(h == 600, "Got unexpected screen height %u.\n", h);
3402 ddraw2 = create_ddraw();
3403 hr = set_display_mode(ddraw2, 640, 480);
3404 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3405 w = GetSystemMetrics(SM_CXSCREEN);
3406 ok(w == 640, "Got unexpected screen width %u.\n", w);
3407 h = GetSystemMetrics(SM_CYSCREEN);
3408 ok(h == 480, "Got unexpected screen height %u.\n", h);
3410 ref = IDirectDraw4_Release(ddraw1);
3411 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3412 w = GetSystemMetrics(SM_CXSCREEN);
3413 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3414 h = GetSystemMetrics(SM_CYSCREEN);
3415 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3417 ref = IDirectDraw4_Release(ddraw2);
3418 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3419 w = GetSystemMetrics(SM_CXSCREEN);
3420 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3421 h = GetSystemMetrics(SM_CYSCREEN);
3422 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3424 /* But only for ddraw objects that called SetDisplayMode(). */
3425 ddraw1 = create_ddraw();
3426 ddraw2 = create_ddraw();
3427 hr = set_display_mode(ddraw2, 640, 480);
3428 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3429 w = GetSystemMetrics(SM_CXSCREEN);
3430 ok(w == 640, "Got unexpected screen width %u.\n", w);
3431 h = GetSystemMetrics(SM_CYSCREEN);
3432 ok(h == 480, "Got unexpected screen height %u.\n", h);
3434 ref = IDirectDraw4_Release(ddraw1);
3435 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3436 w = GetSystemMetrics(SM_CXSCREEN);
3437 ok(w == 640, "Got unexpected screen width %u.\n", w);
3438 h = GetSystemMetrics(SM_CYSCREEN);
3439 ok(h == 480, "Got unexpected screen height %u.\n", h);
3441 ref = IDirectDraw4_Release(ddraw2);
3442 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3443 w = GetSystemMetrics(SM_CXSCREEN);
3444 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3445 h = GetSystemMetrics(SM_CYSCREEN);
3446 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3448 /* If there's a ddraw object that's currently in exclusive mode, it blocks
3449 * restoring the display mode. */
3450 ddraw1 = create_ddraw();
3451 hr = set_display_mode(ddraw1, 800, 600);
3452 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3453 w = GetSystemMetrics(SM_CXSCREEN);
3454 ok(w == 800, "Got unexpected screen width %u.\n", w);
3455 h = GetSystemMetrics(SM_CYSCREEN);
3456 ok(h == 600, "Got unexpected screen height %u.\n", h);
3458 ddraw2 = create_ddraw();
3459 hr = set_display_mode(ddraw2, 640, 480);
3460 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3461 w = GetSystemMetrics(SM_CXSCREEN);
3462 ok(w == 640, "Got unexpected screen width %u.\n", w);
3463 h = GetSystemMetrics(SM_CYSCREEN);
3464 ok(h == 480, "Got unexpected screen height %u.\n", h);
3466 hr = IDirectDraw4_SetCooperativeLevel(ddraw2, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3467 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3469 ref = IDirectDraw4_Release(ddraw1);
3470 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3471 w = GetSystemMetrics(SM_CXSCREEN);
3472 ok(w == 640, "Got unexpected screen width %u.\n", w);
3473 h = GetSystemMetrics(SM_CYSCREEN);
3474 ok(h == 480, "Got unexpected screen height %u.\n", h);
3476 ref = IDirectDraw4_Release(ddraw2);
3477 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3478 w = GetSystemMetrics(SM_CXSCREEN);
3479 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3480 h = GetSystemMetrics(SM_CYSCREEN);
3481 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3483 /* Exclusive mode blocks mode setting on other ddraw objects in general. */
3484 ddraw1 = create_ddraw();
3485 hr = set_display_mode(ddraw1, 800, 600);
3486 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3487 w = GetSystemMetrics(SM_CXSCREEN);
3488 ok(w == 800, "Got unexpected screen width %u.\n", w);
3489 h = GetSystemMetrics(SM_CYSCREEN);
3490 ok(h == 600, "Got unexpected screen height %u.\n", h);
3492 hr = IDirectDraw4_SetCooperativeLevel(ddraw1, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3493 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3495 ddraw2 = create_ddraw();
3496 hr = set_display_mode(ddraw2, 640, 480);
3497 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
3499 ref = IDirectDraw4_Release(ddraw1);
3500 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3501 w = GetSystemMetrics(SM_CXSCREEN);
3502 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3503 h = GetSystemMetrics(SM_CYSCREEN);
3504 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3506 ref = IDirectDraw4_Release(ddraw2);
3507 ok(ref == 0, "The ddraw object was not properly freed: refcount %u.\n", ref);
3508 w = GetSystemMetrics(SM_CXSCREEN);
3509 ok(w == registry_mode.dmPelsWidth, "Got unexpected screen width %u.\n", w);
3510 h = GetSystemMetrics(SM_CYSCREEN);
3511 ok(h == registry_mode.dmPelsHeight, "Got unexpected screen height %u.\n", h);
3513 DestroyWindow(window);
3516 static void test_initialize(void)
3518 IDirectDraw4 *ddraw;
3519 HRESULT hr;
3521 ddraw = create_ddraw();
3522 ok(!!ddraw, "Failed to create a ddraw object.\n");
3524 hr = IDirectDraw4_Initialize(ddraw, NULL);
3525 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x.\n", hr);
3526 IDirectDraw4_Release(ddraw);
3528 CoInitialize(NULL);
3529 hr = CoCreateInstance(&CLSID_DirectDraw, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectDraw4, (void **)&ddraw);
3530 ok(SUCCEEDED(hr), "Failed to create IDirectDraw4 instance, hr %#x.\n", hr);
3531 hr = IDirectDraw4_Initialize(ddraw, NULL);
3532 ok(hr == DD_OK, "Initialize returned hr %#x, expected DD_OK.\n", hr);
3533 hr = IDirectDraw4_Initialize(ddraw, NULL);
3534 ok(hr == DDERR_ALREADYINITIALIZED, "Initialize returned hr %#x, expected DDERR_ALREADYINITIALIZED.\n", hr);
3535 IDirectDraw4_Release(ddraw);
3536 CoUninitialize();
3539 static void test_coop_level_surf_create(void)
3541 IDirectDrawSurface4 *surface;
3542 IDirectDraw4 *ddraw;
3543 DDSURFACEDESC2 ddsd;
3544 HRESULT hr;
3546 ddraw = create_ddraw();
3547 ok(!!ddraw, "Failed to create a ddraw object.\n");
3549 memset(&ddsd, 0, sizeof(ddsd));
3550 ddsd.dwSize = sizeof(ddsd);
3551 ddsd.dwFlags = DDSD_CAPS;
3552 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
3553 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
3554 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "Surface creation returned hr %#x.\n", hr);
3556 IDirectDraw4_Release(ddraw);
3559 static void test_vb_discard(void)
3561 static const struct vec4 quad[] =
3563 { 0.0f, 480.0f, 0.0f, 1.0f},
3564 { 0.0f, 0.0f, 0.0f, 1.0f},
3565 {640.0f, 480.0f, 0.0f, 1.0f},
3566 {640.0f, 0.0f, 0.0f, 1.0f},
3569 IDirect3DDevice3 *device;
3570 IDirect3D3 *d3d;
3571 IDirect3DVertexBuffer *buffer;
3572 HWND window;
3573 HRESULT hr;
3574 D3DVERTEXBUFFERDESC desc;
3575 BYTE *data;
3576 static const unsigned int vbsize = 16;
3577 unsigned int i;
3579 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3580 0, 0, 640, 480, 0, 0, 0, 0);
3582 if (!(device = create_device(window, DDSCL_NORMAL)))
3584 skip("Failed to create a 3D device, skipping test.\n");
3585 DestroyWindow(window);
3586 return;
3589 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
3590 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
3592 memset(&desc, 0, sizeof(desc));
3593 desc.dwSize = sizeof(desc);
3594 desc.dwCaps = D3DVBCAPS_WRITEONLY;
3595 desc.dwFVF = D3DFVF_XYZRHW;
3596 desc.dwNumVertices = vbsize;
3597 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &buffer, 0, NULL);
3598 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
3600 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3601 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3602 memcpy(data, quad, sizeof(quad));
3603 hr = IDirect3DVertexBuffer_Unlock(buffer);
3604 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3606 hr = IDirect3DDevice3_BeginScene(device);
3607 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3608 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
3609 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3610 hr = IDirect3DDevice3_EndScene(device);
3611 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3613 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3614 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3615 memset(data, 0xaa, sizeof(struct vec4) * vbsize);
3616 hr = IDirect3DVertexBuffer_Unlock(buffer);
3617 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3619 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, (void **)&data, NULL);
3620 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
3621 for (i = 0; i < sizeof(struct vec4) * vbsize; i++)
3623 if (data[i] != 0xaa)
3625 ok(FALSE, "Vertex buffer data byte %u is 0x%02x, expected 0xaa\n", i, data[i]);
3626 break;
3629 hr = IDirect3DVertexBuffer_Unlock(buffer);
3630 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
3632 IDirect3DVertexBuffer_Release(buffer);
3633 IDirect3D3_Release(d3d);
3634 IDirect3DDevice3_Release(device);
3635 DestroyWindow(window);
3638 static void test_coop_level_multi_window(void)
3640 HWND window1, window2;
3641 IDirectDraw4 *ddraw;
3642 HRESULT hr;
3644 window1 = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3645 0, 0, 640, 480, 0, 0, 0, 0);
3646 window2 = CreateWindowA("static", "ddraw_test2", WS_OVERLAPPEDWINDOW,
3647 0, 0, 640, 480, 0, 0, 0, 0);
3648 ddraw = create_ddraw();
3649 ok(!!ddraw, "Failed to create a ddraw object.\n");
3651 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window1, DDSCL_NORMAL);
3652 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3653 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window2, DDSCL_NORMAL);
3654 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
3655 ok(IsWindow(window1), "Window 1 was destroyed.\n");
3656 ok(IsWindow(window2), "Window 2 was destroyed.\n");
3658 IDirectDraw4_Release(ddraw);
3659 DestroyWindow(window2);
3660 DestroyWindow(window1);
3663 static void test_draw_strided(void)
3665 static struct vec3 position[] =
3667 {-1.0, -1.0, 0.0},
3668 {-1.0, 1.0, 0.0},
3669 { 1.0, 1.0, 0.0},
3670 { 1.0, -1.0, 0.0},
3672 static DWORD diffuse[] =
3674 0x0000ff00, 0x0000ff00, 0x0000ff00, 0x0000ff00,
3676 static WORD indices[] =
3678 0, 1, 2, 2, 3, 0
3681 IDirectDrawSurface4 *rt;
3682 IDirect3DDevice3 *device;
3683 D3DCOLOR color;
3684 HWND window;
3685 HRESULT hr;
3686 D3DDRAWPRIMITIVESTRIDEDDATA strided;
3687 IDirect3DViewport3 *viewport;
3688 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3690 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3691 0, 0, 640, 480, 0, 0, 0, 0);
3693 if (!(device = create_device(window, DDSCL_NORMAL)))
3695 skip("Failed to create a 3D device, skipping test.\n");
3696 DestroyWindow(window);
3697 return;
3700 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
3701 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3702 viewport = create_viewport(device, 0, 0, 640, 480);
3703 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
3704 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
3705 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
3706 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3708 hr = IDirect3DDevice3_BeginScene(device);
3709 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
3711 memset(&strided, 0x55, sizeof(strided));
3712 strided.position.lpvData = position;
3713 strided.position.dwStride = sizeof(*position);
3714 strided.diffuse.lpvData = diffuse;
3715 strided.diffuse.dwStride = sizeof(*diffuse);
3716 hr = IDirect3DDevice3_DrawIndexedPrimitiveStrided(device, D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE,
3717 &strided, 4, indices, 6, 0);
3718 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
3720 hr = IDirect3DDevice3_EndScene(device);
3721 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
3723 color = get_surface_color(rt, 320, 240);
3724 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
3726 IDirect3DViewport3_Release(viewport);
3727 IDirectDrawSurface4_Release(rt);
3728 IDirect3DDevice3_Release(device);
3729 DestroyWindow(window);
3732 static void test_clear_rect_count(void)
3734 IDirectDrawSurface4 *rt;
3735 IDirect3DDevice3 *device;
3736 D3DCOLOR color;
3737 HWND window;
3738 HRESULT hr;
3739 IDirect3DViewport3 *viewport;
3740 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3742 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
3743 0, 0, 640, 480, 0, 0, 0, 0);
3744 if (!(device = create_device(window, DDSCL_NORMAL)))
3746 skip("Failed to create a 3D device, skipping test.\n");
3747 DestroyWindow(window);
3748 return;
3751 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
3752 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
3754 viewport = create_viewport(device, 0, 0, 640, 480);
3755 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
3756 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
3757 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00ffffff, 0.0f, 0);
3758 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3759 hr = IDirect3DViewport3_Clear2(viewport, 0, &clear_rect, D3DCLEAR_TARGET, 0x00ff0000, 0.0f, 0);
3760 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3761 hr = IDirect3DViewport3_Clear2(viewport, 0, NULL, D3DCLEAR_TARGET, 0x0000ff00, 0.0f, 0);
3762 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3763 hr = IDirect3DViewport3_Clear2(viewport, 1, NULL, D3DCLEAR_TARGET, 0x000000ff, 0.0f, 0);
3764 ok(SUCCEEDED(hr), "Failed to clear the viewport, hr %#x.\n", hr);
3766 color = get_surface_color(rt, 320, 240);
3767 ok(compare_color(color, 0x00ffffff, 1), "Got unexpected color 0x%08x.\n", color);
3769 IDirect3DViewport3_Release(viewport);
3770 IDirectDrawSurface4_Release(rt);
3771 IDirect3DDevice3_Release(device);
3772 DestroyWindow(window);
3775 static BOOL test_mode_restored(IDirectDraw4 *ddraw, HWND window)
3777 DDSURFACEDESC2 ddsd1, ddsd2;
3778 HRESULT hr;
3780 memset(&ddsd1, 0, sizeof(ddsd1));
3781 ddsd1.dwSize = sizeof(ddsd1);
3782 hr = IDirectDraw4_GetDisplayMode(ddraw, &ddsd1);
3783 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
3785 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
3786 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3787 hr = set_display_mode(ddraw, 640, 480);
3788 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
3789 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
3790 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3792 memset(&ddsd2, 0, sizeof(ddsd2));
3793 ddsd2.dwSize = sizeof(ddsd2);
3794 hr = IDirectDraw4_GetDisplayMode(ddraw, &ddsd2);
3795 ok(SUCCEEDED(hr), "GetDisplayMode failed, hr %#x.\n", hr);
3796 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
3797 ok(SUCCEEDED(hr), "RestoreDisplayMode failed, hr %#x.\n", hr);
3799 return ddsd1.dwWidth == ddsd2.dwWidth && ddsd1.dwHeight == ddsd2.dwHeight;
3802 static void test_coop_level_versions(void)
3804 HWND window;
3805 IDirectDraw *ddraw;
3806 HRESULT hr;
3807 BOOL restored;
3808 IDirectDrawSurface *surface;
3809 IDirectDraw4 *ddraw4;
3810 DDSURFACEDESC ddsd;
3812 window = CreateWindowA("static", "ddraw_test1", WS_OVERLAPPEDWINDOW,
3813 0, 0, 640, 480, 0, 0, 0, 0);
3815 ddraw4 = create_ddraw();
3816 ok(!!ddraw4, "Failed to create a ddraw object.\n");
3817 /* Newly created ddraw objects restore the mode on ddraw2+::SetCooperativeLevel(NORMAL) */
3818 restored = test_mode_restored(ddraw4, window);
3819 ok(restored, "Display mode not restored in new ddraw object\n");
3821 /* A failing ddraw1::SetCooperativeLevel call does not have an effect */
3822 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
3823 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3825 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3826 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
3827 restored = test_mode_restored(ddraw4, window);
3828 ok(restored, "Display mode not restored after bad ddraw1::SetCooperativeLevel call\n");
3830 /* A successful one does */
3831 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3832 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3833 restored = test_mode_restored(ddraw4, window);
3834 ok(!restored, "Display mode restored after good ddraw1::SetCooperativeLevel call\n");
3836 IDirectDraw_Release(ddraw);
3837 IDirectDraw4_Release(ddraw4);
3839 ddraw4 = create_ddraw();
3840 ok(!!ddraw4, "Failed to create a ddraw object.\n");
3841 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
3842 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3844 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_SETFOCUSWINDOW);
3845 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3846 restored = test_mode_restored(ddraw4, window);
3847 ok(!restored, "Display mode restored after ddraw1::SetCooperativeLevel(SETFOCUSWINDOW) call\n");
3849 IDirectDraw_Release(ddraw);
3850 IDirectDraw4_Release(ddraw4);
3852 /* A failing call does not restore the ddraw2+ behavior */
3853 ddraw4 = create_ddraw();
3854 ok(!!ddraw4, "Failed to create a ddraw object.\n");
3855 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
3856 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3858 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3859 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3860 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3861 ok(FAILED(hr), "SetCooperativeLevel returned %#x, expected failure.\n", hr);
3862 restored = test_mode_restored(ddraw4, window);
3863 ok(!restored, "Display mode restored after good-bad ddraw1::SetCooperativeLevel() call sequence\n");
3865 IDirectDraw_Release(ddraw);
3866 IDirectDraw4_Release(ddraw4);
3868 /* Neither does a sequence of successful calls with the new interface */
3869 ddraw4 = create_ddraw();
3870 ok(!!ddraw4, "Failed to create a ddraw object.\n");
3871 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
3872 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3874 hr = IDirectDraw_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
3875 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3876 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
3877 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3878 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_NORMAL);
3879 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3881 restored = test_mode_restored(ddraw4, window);
3882 ok(!restored, "Display mode restored after ddraw1-ddraw4 SetCooperativeLevel() call sequence\n");
3883 IDirectDraw_Release(ddraw);
3884 IDirectDraw4_Release(ddraw4);
3886 /* ddraw1::CreateSurface does not triger the ddraw1 behavior */
3887 ddraw4 = create_ddraw();
3888 ok(!!ddraw4, "Failed to create a ddraw object.\n");
3889 hr = IDirectDraw4_QueryInterface(ddraw4, &IID_IDirectDraw, (void **)&ddraw);
3890 ok(SUCCEEDED(hr), "QueryInterface failed, hr %#x.\n", hr);
3892 hr = IDirectDraw4_SetCooperativeLevel(ddraw4, window, DDSCL_NORMAL);
3893 ok(SUCCEEDED(hr), "SetCooperativeLevel failed, hr %#x.\n", hr);
3895 memset(&ddsd, 0, sizeof(ddsd));
3896 ddsd.dwSize = sizeof(ddsd);
3897 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3898 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3899 ddsd.dwWidth = ddsd.dwHeight = 8;
3900 hr = IDirectDraw_CreateSurface(ddraw, &ddsd, &surface, NULL);
3901 ok(SUCCEEDED(hr), "CreateSurface failed, hr %#x.\n", hr);
3902 IDirectDrawSurface_Release(surface);
3903 restored = test_mode_restored(ddraw4, window);
3904 ok(restored, "Display mode not restored after ddraw1::CreateSurface() call\n");
3906 IDirectDraw_Release(ddraw);
3907 IDirectDraw4_Release(ddraw4);
3908 DestroyWindow(window);
3911 static void test_lighting_interface_versions(void)
3913 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
3914 IDirect3DMaterial3 *emissive;
3915 IDirect3DViewport3 *viewport;
3916 IDirect3DDevice3 *device;
3917 IDirectDrawSurface4 *rt;
3918 D3DCOLOR color;
3919 HWND window;
3920 HRESULT hr;
3921 D3DMATERIALHANDLE mat_handle;
3922 DWORD rs;
3923 unsigned int i;
3924 ULONG ref;
3925 static D3DVERTEX quad[] =
3927 {{-1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3928 {{ 1.0f}, { 1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3929 {{-1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3930 {{ 1.0f}, {-1.0f}, {0.0f}, {1.0f}, {0.0f}, {0.0f}},
3933 #define FVF_COLORVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR)
3934 static struct
3936 struct vec3 position;
3937 struct vec3 normal;
3938 DWORD diffuse, specular;
3940 quad2[] =
3942 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3943 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3944 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3945 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000, 0xff808080},
3948 static D3DLVERTEX lquad[] =
3950 {{-1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3951 {{ 1.0f}, { 1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3952 {{-1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3953 {{ 1.0f}, {-1.0f}, {0.0f}, 0, {0xffff0000}, {0xff808080}},
3956 #define FVF_LVERTEX2 (D3DFVF_LVERTEX & ~D3DFVF_RESERVED1)
3957 static struct
3959 struct vec3 position;
3960 DWORD diffuse, specular;
3961 struct vec2 texcoord;
3963 lquad2[] =
3965 {{-1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
3966 {{ 1.0f, 1.0f, 0.0f}, 0xffff0000, 0xff808080},
3967 {{-1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
3968 {{ 1.0f, -1.0f, 0.0f}, 0xffff0000, 0xff808080},
3971 static D3DTLVERTEX tlquad[] =
3973 {{ 0.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3974 {{ 0.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3975 {{ 640.0f}, { 480.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3976 {{ 640.0f}, { 0.0f}, {0.0f}, {1.0f}, {0xff0000ff}, {0xff808080}},
3979 static const struct
3981 DWORD vertextype;
3982 void *data;
3983 DWORD d3drs_lighting, d3drs_specular;
3984 DWORD draw_flags;
3985 D3DCOLOR color;
3987 tests[] =
3989 /* Lighting is enabled when all of these conditions are met:
3990 * 1) No pretransformed position(D3DFVF_XYZRHW)
3991 * 2) Normals are available (D3DFVF_NORMAL)
3992 * 3) D3DDP_DONOTLIGHT is not set.
3994 * D3DRENDERSTATE_LIGHTING is ignored, it is not defined
3995 * in this d3d version */
3997 /* 0 */
3998 { D3DFVF_VERTEX, quad, FALSE, FALSE, 0, 0x0000ff00},
3999 { D3DFVF_VERTEX, quad, TRUE, FALSE, 0, 0x0000ff00},
4000 { D3DFVF_VERTEX, quad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
4001 { D3DFVF_VERTEX, quad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ffffff},
4002 { D3DFVF_VERTEX, quad, FALSE, TRUE, 0, 0x0000ff00},
4003 { D3DFVF_VERTEX, quad, TRUE, TRUE, 0, 0x0000ff00},
4004 { D3DFVF_VERTEX, quad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
4005 { D3DFVF_VERTEX, quad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ffffff},
4007 /* 8 */
4008 { FVF_COLORVERTEX, quad2, FALSE, FALSE, 0, 0x0000ff00},
4009 { FVF_COLORVERTEX, quad2, TRUE, FALSE, 0, 0x0000ff00},
4010 { FVF_COLORVERTEX, quad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4011 { FVF_COLORVERTEX, quad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4012 /* The specular color in the vertex is ignored because
4013 * D3DRENDERSTATE_COLORVERTEX is not enabled */
4014 { FVF_COLORVERTEX, quad2, FALSE, TRUE, 0, 0x0000ff00},
4015 { FVF_COLORVERTEX, quad2, TRUE, TRUE, 0, 0x0000ff00},
4016 { FVF_COLORVERTEX, quad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4017 { FVF_COLORVERTEX, quad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4019 /* 16 */
4020 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, 0, 0x00ff0000},
4021 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, 0, 0x00ff0000},
4022 { D3DFVF_LVERTEX, lquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4023 { D3DFVF_LVERTEX, lquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4024 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, 0, 0x00ff8080},
4025 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, 0, 0x00ff8080},
4026 { D3DFVF_LVERTEX, lquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4027 { D3DFVF_LVERTEX, lquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4029 /* 24 */
4030 { FVF_LVERTEX2, lquad2, FALSE, FALSE, 0, 0x00ff0000},
4031 { FVF_LVERTEX2, lquad2, TRUE, FALSE, 0, 0x00ff0000},
4032 { FVF_LVERTEX2, lquad2, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4033 { FVF_LVERTEX2, lquad2, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x00ff0000},
4034 { FVF_LVERTEX2, lquad2, FALSE, TRUE, 0, 0x00ff8080},
4035 { FVF_LVERTEX2, lquad2, TRUE, TRUE, 0, 0x00ff8080},
4036 { FVF_LVERTEX2, lquad2, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4037 { FVF_LVERTEX2, lquad2, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x00ff8080},
4039 /* 32 */
4040 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, 0, 0x000000ff},
4041 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, 0, 0x000000ff},
4042 { D3DFVF_TLVERTEX, tlquad, FALSE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
4043 { D3DFVF_TLVERTEX, tlquad, TRUE, FALSE, D3DDP_DONOTLIGHT, 0x000000ff},
4044 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, 0, 0x008080ff},
4045 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, 0, 0x008080ff},
4046 { D3DFVF_TLVERTEX, tlquad, FALSE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
4047 { D3DFVF_TLVERTEX, tlquad, TRUE, TRUE, D3DDP_DONOTLIGHT, 0x008080ff},
4050 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4051 0, 0, 640, 480, 0, 0, 0, 0);
4053 if (!(device = create_device(window, DDSCL_NORMAL)))
4055 skip("Failed to create a 3D device, skipping test.\n");
4056 DestroyWindow(window);
4057 return;
4060 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
4061 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
4063 viewport = create_viewport(device, 0, 0, 640, 480);
4064 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
4065 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
4067 emissive = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
4068 hr = IDirect3DMaterial3_GetHandle(emissive, device, &mat_handle);
4069 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
4070 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
4071 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
4072 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
4073 ok(SUCCEEDED(hr), "Failed to disable z test, hr %#x.\n", hr);
4075 hr = IDirect3DDevice3_GetRenderState(device, D3DRENDERSTATE_SPECULARENABLE, &rs);
4076 ok(SUCCEEDED(hr), "Failed to get specularenable render state, hr %#x.\n", hr);
4077 ok(rs == FALSE, "Initial D3DRENDERSTATE_SPECULARENABLE is %#x, expected FALSE.\n", rs);
4079 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4081 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0xff202020, 0.0f, 0);
4082 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
4084 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_LIGHTING, tests[i].d3drs_lighting);
4085 ok(SUCCEEDED(hr), "Failed to set lighting render state, hr %#x.\n", hr);
4086 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_SPECULARENABLE,
4087 tests[i].d3drs_specular);
4088 ok(SUCCEEDED(hr), "Failed to set specularenable render state, hr %#x.\n", hr);
4090 hr = IDirect3DDevice3_BeginScene(device);
4091 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
4092 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
4093 tests[i].vertextype, tests[i].data, 4, tests[i].draw_flags | D3DDP_WAIT);
4094 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
4095 hr = IDirect3DDevice3_EndScene(device);
4096 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
4098 color = get_surface_color(rt, 320, 240);
4099 ok(compare_color(color, tests[i].color, 1),
4100 "Got unexpected color 0x%08x, expected 0x%08x, test %u.\n",
4101 color, tests[i].color, i);
4104 destroy_material(emissive);
4105 IDirectDrawSurface4_Release(rt);
4106 ref = IDirect3DDevice3_Release(device);
4107 ok(ref == 0, "Device not properly released, refcount %u.\n", ref);
4108 DestroyWindow(window);
4111 static struct
4113 BOOL received;
4114 IDirectDraw4 *ddraw;
4115 HWND window;
4116 DWORD coop_level;
4117 } activateapp_testdata;
4119 static LRESULT CALLBACK activateapp_test_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
4121 if (message == WM_ACTIVATEAPP)
4123 if (activateapp_testdata.ddraw)
4125 HRESULT hr;
4126 activateapp_testdata.received = FALSE;
4127 hr = IDirectDraw4_SetCooperativeLevel(activateapp_testdata.ddraw,
4128 activateapp_testdata.window, activateapp_testdata.coop_level);
4129 ok(SUCCEEDED(hr), "Recursive SetCooperativeLevel call failed, hr %#x.\n", hr);
4130 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP during recursive SetCooperativeLevel call.\n");
4132 activateapp_testdata.received = TRUE;
4135 return DefWindowProcA(hwnd, message, wparam, lparam);
4138 static void test_coop_level_activateapp(void)
4140 IDirectDraw4 *ddraw;
4141 HRESULT hr;
4142 HWND window;
4143 WNDCLASSA wc = {0};
4144 DDSURFACEDESC2 ddsd;
4145 IDirectDrawSurface4 *surface;
4147 ddraw = create_ddraw();
4148 ok(!!ddraw, "Failed to create a ddraw object.\n");
4150 wc.lpfnWndProc = activateapp_test_proc;
4151 wc.lpszClassName = "ddraw_test_wndproc_wc";
4152 ok(RegisterClassA(&wc), "Failed to register window class.\n");
4154 window = CreateWindowA("ddraw_test_wndproc_wc", "ddraw_test",
4155 WS_MAXIMIZE | WS_CAPTION , 0, 0, 640, 480, 0, 0, 0, 0);
4157 /* Exclusive with window already active. */
4158 SetForegroundWindow(window);
4159 activateapp_testdata.received = FALSE;
4160 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4161 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4162 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP although window was already active.\n");
4163 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4164 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4166 /* Exclusive with window not active. */
4167 SetForegroundWindow(GetDesktopWindow());
4168 activateapp_testdata.received = FALSE;
4169 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4170 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4171 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4172 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4173 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4175 /* Normal with window not active, then exclusive with the same window. */
4176 SetForegroundWindow(GetDesktopWindow());
4177 activateapp_testdata.received = FALSE;
4178 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4179 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4180 ok(!activateapp_testdata.received, "Received WM_ACTIVATEAPP when setting DDSCL_NORMAL.\n");
4181 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4182 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4183 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4184 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4185 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4187 /* Recursive set of DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN. */
4188 SetForegroundWindow(GetDesktopWindow());
4189 activateapp_testdata.received = FALSE;
4190 activateapp_testdata.ddraw = ddraw;
4191 activateapp_testdata.window = window;
4192 activateapp_testdata.coop_level = DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN;
4193 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4194 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4195 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4196 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4197 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4199 /* The recursive call seems to have some bad effect on native ddraw, despite (apparently)
4200 * succeeding. Another switch to exclusive and back to normal is needed to release the
4201 * window properly. Without doing this, SetCooperativeLevel(EXCLUSIVE) will not send
4202 * WM_ACTIVATEAPP messages. */
4203 activateapp_testdata.ddraw = NULL;
4204 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4205 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4206 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4207 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4209 /* Setting DDSCL_NORMAL with recursive invocation. */
4210 SetForegroundWindow(GetDesktopWindow());
4211 activateapp_testdata.received = FALSE;
4212 activateapp_testdata.ddraw = ddraw;
4213 activateapp_testdata.window = window;
4214 activateapp_testdata.coop_level = DDSCL_NORMAL;
4215 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4216 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4217 ok(activateapp_testdata.received, "Expected WM_ACTIVATEAPP, but did not receive it.\n");
4219 /* DDraw is in exlusive mode now. */
4220 memset(&ddsd, 0, sizeof(ddsd));
4221 ddsd.dwSize = sizeof(ddsd);
4222 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
4223 U5(ddsd).dwBackBufferCount = 1;
4224 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
4225 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4226 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
4227 IDirectDrawSurface4_Release(surface);
4229 /* Recover again, just to be sure. */
4230 activateapp_testdata.ddraw = NULL;
4231 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
4232 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4233 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4234 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4236 DestroyWindow(window);
4237 UnregisterClassA("ddraw_test_wndproc_wc", GetModuleHandleA(NULL));
4238 IDirectDraw4_Release(ddraw);
4241 static void test_texturemanage(void)
4243 IDirectDraw4 *ddraw;
4244 HRESULT hr;
4245 DDSURFACEDESC2 ddsd;
4246 IDirectDrawSurface4 *surface;
4247 unsigned int i;
4248 DDCAPS hal_caps, hel_caps;
4249 DWORD needed_caps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
4250 static const struct
4252 DWORD caps_in, caps2_in;
4253 HRESULT hr;
4254 DWORD caps_out, caps2_out;
4256 tests[] =
4258 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4259 ~0U, ~0U},
4260 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4261 ~0U, ~0U},
4262 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4263 ~0U, ~0U},
4264 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4265 ~0U, ~0U},
4266 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, DD_OK,
4267 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE},
4268 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, DD_OK,
4269 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE},
4270 {DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
4271 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE | DDSCAPS_LOCALVIDMEM, 0},
4272 {DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0, DD_OK,
4273 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0},
4275 {0, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4276 ~0U, ~0U},
4277 {0, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4278 ~0U, ~0U},
4279 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4280 ~0U, ~0U},
4281 {DDSCAPS_SYSTEMMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4282 ~0U, ~0U},
4283 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_TEXTUREMANAGE, DDERR_INVALIDCAPS,
4284 ~0U, ~0U},
4285 {DDSCAPS_VIDEOMEMORY, DDSCAPS2_D3DTEXTUREMANAGE, DDERR_INVALIDCAPS,
4286 ~0U, ~0U},
4287 {DDSCAPS_VIDEOMEMORY, 0, DD_OK,
4288 DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY, 0},
4289 {DDSCAPS_SYSTEMMEMORY, 0, DD_OK,
4290 DDSCAPS_SYSTEMMEMORY, 0},
4293 ddraw = create_ddraw();
4294 ok(!!ddraw, "Failed to create a ddraw object.\n");
4295 hr = IDirectDraw4_SetCooperativeLevel(ddraw, NULL, DDSCL_NORMAL);
4296 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4298 memset(&hal_caps, 0, sizeof(hal_caps));
4299 hal_caps.dwSize = sizeof(hal_caps);
4300 memset(&hel_caps, 0, sizeof(hel_caps));
4301 hel_caps.dwSize = sizeof(hel_caps);
4302 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, &hel_caps);
4303 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4304 if ((hal_caps.ddsCaps.dwCaps & needed_caps) != needed_caps)
4306 skip("Managed textures not supported, skipping managed texture test.\n");
4307 IDirectDraw4_Release(ddraw);
4308 return;
4311 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
4313 memset(&ddsd, 0, sizeof(ddsd));
4314 ddsd.dwSize = sizeof(ddsd);
4315 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4316 ddsd.ddsCaps.dwCaps = tests[i].caps_in;
4317 ddsd.ddsCaps.dwCaps2 = tests[i].caps2_in;
4318 ddsd.dwWidth = 4;
4319 ddsd.dwHeight = 4;
4321 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4322 ok(hr == tests[i].hr, "Got unexpected, hr %#x, case %u.\n", hr, i);
4323 if (FAILED(hr))
4324 continue;
4326 memset(&ddsd, 0, sizeof(ddsd));
4327 ddsd.dwSize = sizeof(ddsd);
4328 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
4329 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4331 ok(ddsd.ddsCaps.dwCaps == tests[i].caps_out,
4332 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
4333 tests[i].caps_in, tests[i].caps2_in, tests[i].caps_out, ddsd.ddsCaps.dwCaps, i);
4334 ok(ddsd.ddsCaps.dwCaps2 == tests[i].caps2_out,
4335 "Input caps %#x, %#x, expected output caps %#x, got %#x, case %u.\n",
4336 tests[i].caps_in, tests[i].caps2_in, tests[i].caps2_out, ddsd.ddsCaps.dwCaps2, i);
4338 IDirectDrawSurface4_Release(surface);
4341 IDirectDraw4_Release(ddraw);
4344 #define SUPPORT_DXT1 0x01
4345 #define SUPPORT_DXT2 0x02
4346 #define SUPPORT_DXT3 0x04
4347 #define SUPPORT_DXT4 0x08
4348 #define SUPPORT_DXT5 0x10
4349 #define SUPPORT_YUY2 0x20
4350 #define SUPPORT_UYVY 0x40
4352 static HRESULT WINAPI test_block_formats_creation_cb(DDPIXELFORMAT *fmt, void *ctx)
4354 DWORD *supported_fmts = ctx;
4356 if (!(fmt->dwFlags & DDPF_FOURCC))
4357 return DDENUMRET_OK;
4359 switch (fmt->dwFourCC)
4361 case MAKEFOURCC('D','X','T','1'):
4362 *supported_fmts |= SUPPORT_DXT1;
4363 break;
4364 case MAKEFOURCC('D','X','T','2'):
4365 *supported_fmts |= SUPPORT_DXT2;
4366 break;
4367 case MAKEFOURCC('D','X','T','3'):
4368 *supported_fmts |= SUPPORT_DXT3;
4369 break;
4370 case MAKEFOURCC('D','X','T','4'):
4371 *supported_fmts |= SUPPORT_DXT4;
4372 break;
4373 case MAKEFOURCC('D','X','T','5'):
4374 *supported_fmts |= SUPPORT_DXT5;
4375 break;
4376 case MAKEFOURCC('Y','U','Y','2'):
4377 *supported_fmts |= SUPPORT_YUY2;
4378 break;
4379 case MAKEFOURCC('U','Y','V','Y'):
4380 *supported_fmts |= SUPPORT_UYVY;
4381 break;
4382 default:
4383 break;
4386 return DDENUMRET_OK;
4389 static void test_block_formats_creation(void)
4391 HRESULT hr, expect_hr;
4392 unsigned int i, j, w, h;
4393 HWND window;
4394 IDirectDraw4 *ddraw;
4395 IDirect3D3 *d3d;
4396 IDirect3DDevice3 *device;
4397 IDirectDrawSurface4 *surface;
4398 DWORD supported_fmts = 0, supported_overlay_fmts = 0;
4399 DWORD num_fourcc_codes = 0, *fourcc_codes;
4400 DDSURFACEDESC2 ddsd;
4401 DDCAPS hal_caps;
4402 void *mem;
4404 static const struct
4406 DWORD fourcc;
4407 const char *name;
4408 DWORD support_flag;
4409 unsigned int block_width;
4410 unsigned int block_height;
4411 unsigned int block_size;
4412 BOOL create_size_checked, overlay;
4414 formats[] =
4416 {MAKEFOURCC('D','X','T','1'), "D3DFMT_DXT1", SUPPORT_DXT1, 4, 4, 8, TRUE, FALSE},
4417 {MAKEFOURCC('D','X','T','2'), "D3DFMT_DXT2", SUPPORT_DXT2, 4, 4, 16, TRUE, FALSE},
4418 {MAKEFOURCC('D','X','T','3'), "D3DFMT_DXT3", SUPPORT_DXT3, 4, 4, 16, TRUE, FALSE},
4419 {MAKEFOURCC('D','X','T','4'), "D3DFMT_DXT4", SUPPORT_DXT4, 4, 4, 16, TRUE, FALSE},
4420 {MAKEFOURCC('D','X','T','5'), "D3DFMT_DXT5", SUPPORT_DXT5, 4, 4, 16, TRUE, FALSE},
4421 {MAKEFOURCC('Y','U','Y','2'), "D3DFMT_YUY2", SUPPORT_YUY2, 2, 1, 4, FALSE, TRUE },
4422 {MAKEFOURCC('U','Y','V','Y'), "D3DFMT_UYVY", SUPPORT_UYVY, 2, 1, 4, FALSE, TRUE },
4424 static const struct
4426 DWORD caps, caps2;
4427 const char *name;
4428 BOOL overlay;
4430 types[] =
4432 /* DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY fails to create any fourcc
4433 * surface with DDERR_INVALIDPIXELFORMAT. Don't care about it for now.
4435 * Nvidia returns E_FAIL on DXTN DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY.
4436 * Other hw / drivers successfully create those surfaces. Ignore them, this
4437 * suggests that no game uses this, otherwise Nvidia would support it. */
4439 DDSCAPS_VIDEOMEMORY | DDSCAPS_TEXTURE, 0,
4440 "videomemory texture", FALSE
4443 DDSCAPS_VIDEOMEMORY | DDSCAPS_OVERLAY, 0,
4444 "videomemory overlay", TRUE
4447 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE, 0,
4448 "systemmemory texture", FALSE
4451 DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE,
4452 "managed texture", FALSE
4455 enum size_type
4457 SIZE_TYPE_ZERO,
4458 SIZE_TYPE_PITCH,
4459 SIZE_TYPE_SIZE,
4461 static const struct
4463 DWORD flags;
4464 enum size_type size_type;
4465 int rel_size;
4466 HRESULT hr;
4468 user_mem_tests[] =
4470 {DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DD_OK},
4471 {DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
4472 {DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
4473 {DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
4474 {DDSD_LPSURFACE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
4475 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_ZERO, 0, DDERR_INVALIDPARAMS},
4476 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_PITCH, 0, DDERR_INVALIDPARAMS},
4477 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
4478 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 1, DD_OK},
4479 {DDSD_LPSURFACE | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, -1, DDERR_INVALIDPARAMS},
4480 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_ZERO, 0, DD_OK},
4481 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_PITCH, 0, DD_OK},
4482 {DDSD_LPSURFACE | DDSD_PITCH, SIZE_TYPE_SIZE, 0, DD_OK},
4483 {DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, SIZE_TYPE_SIZE, 0, DD_OK},
4486 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4487 0, 0, 640, 480, 0, 0, 0, 0);
4489 if (!(device = create_device(window, DDSCL_NORMAL)))
4491 skip("Failed to create a 3D device, skipping test.\n");
4492 DestroyWindow(window);
4493 return;
4496 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
4497 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
4498 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **) &ddraw);
4499 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
4500 IDirect3D3_Release(d3d);
4502 hr = IDirect3DDevice3_EnumTextureFormats(device, test_block_formats_creation_cb,
4503 &supported_fmts);
4504 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
4506 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, NULL);
4507 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
4508 fourcc_codes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
4509 num_fourcc_codes * sizeof(*fourcc_codes));
4510 if (!fourcc_codes)
4511 goto cleanup;
4512 hr = IDirectDraw4_GetFourCCCodes(ddraw, &num_fourcc_codes, fourcc_codes);
4513 ok(SUCCEEDED(hr), "Failed to get fourcc codes %#x.\n", hr);
4514 for (i = 0; i < num_fourcc_codes; i++)
4516 for (j = 0; j < sizeof(formats) / sizeof(*formats); j++)
4518 if (fourcc_codes[i] == formats[j].fourcc)
4519 supported_overlay_fmts |= formats[j].support_flag;
4522 HeapFree(GetProcessHeap(), 0, fourcc_codes);
4524 memset(&hal_caps, 0, sizeof(hal_caps));
4525 hal_caps.dwSize = sizeof(hal_caps);
4526 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
4527 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
4529 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2 * 2 * 16 + 1);
4531 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
4533 for (j = 0; j < sizeof(types) / sizeof(*types); j++)
4535 BOOL support;
4537 if (formats[i].overlay != types[j].overlay
4538 || (types[j].overlay && !(hal_caps.dwCaps & DDCAPS_OVERLAY)))
4539 continue;
4541 if (formats[i].overlay)
4542 support = supported_overlay_fmts & formats[i].support_flag;
4543 else
4544 support = supported_fmts & formats[i].support_flag;
4546 for (w = 1; w <= 8; w++)
4548 for (h = 1; h <= 8; h++)
4550 BOOL block_aligned = TRUE;
4551 BOOL todo = FALSE;
4553 if (w & (formats[i].block_width - 1) || h & (formats[i].block_height - 1))
4554 block_aligned = FALSE;
4556 memset(&ddsd, 0, sizeof(ddsd));
4557 ddsd.dwSize = sizeof(ddsd);
4558 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
4559 ddsd.ddsCaps.dwCaps = types[j].caps;
4560 ddsd.ddsCaps.dwCaps2 = types[j].caps2;
4561 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
4562 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
4563 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
4564 ddsd.dwWidth = w;
4565 ddsd.dwHeight = h;
4567 /* TODO: Handle power of two limitations. I cannot test the pow2
4568 * behavior on windows because I have no hardware that doesn't at
4569 * least support np2_conditional. There's probably no HW that
4570 * supports DXTN textures but no conditional np2 textures. */
4571 if (!support && !(types[j].caps & DDSCAPS_SYSTEMMEMORY))
4572 expect_hr = DDERR_INVALIDPARAMS;
4573 else if (formats[i].create_size_checked && !block_aligned)
4575 expect_hr = DDERR_INVALIDPARAMS;
4576 if (!(types[j].caps & DDSCAPS_TEXTURE))
4577 todo = TRUE;
4579 else
4580 expect_hr = D3D_OK;
4582 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4583 if (todo)
4584 todo_wine ok(hr == expect_hr,
4585 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
4586 hr, formats[i].name, types[j].name, w, h, expect_hr);
4587 else
4588 ok(hr == expect_hr,
4589 "Got unexpected hr %#x for format %s, resource type %s, size %ux%u, expected %#x.\n",
4590 hr, formats[i].name, types[j].name, w, h, expect_hr);
4592 if (SUCCEEDED(hr))
4593 IDirectDrawSurface4_Release(surface);
4598 if (formats[i].overlay)
4599 continue;
4601 for (j = 0; j < sizeof(user_mem_tests) / sizeof(*user_mem_tests); ++j)
4603 memset(&ddsd, 0, sizeof(ddsd));
4604 ddsd.dwSize = sizeof(ddsd);
4605 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | user_mem_tests[j].flags;
4606 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE;
4608 switch (user_mem_tests[j].size_type)
4610 case SIZE_TYPE_ZERO:
4611 U1(ddsd).dwLinearSize = 0;
4612 break;
4614 case SIZE_TYPE_PITCH:
4615 U1(ddsd).dwLinearSize = 2 * formats[i].block_size;
4616 break;
4618 case SIZE_TYPE_SIZE:
4619 U1(ddsd).dwLinearSize = 2 * 2 * formats[i].block_size;
4620 break;
4622 U1(ddsd).dwLinearSize += user_mem_tests[j].rel_size;
4624 ddsd.lpSurface = mem;
4625 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
4626 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_FOURCC;
4627 U4(ddsd).ddpfPixelFormat.dwFourCC = formats[i].fourcc;
4628 ddsd.dwWidth = 8;
4629 ddsd.dwHeight = 8;
4631 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4632 ok(hr == user_mem_tests[j].hr, "Test %u: Got unexpected hr %#x, format %s.\n", j, hr, formats[i].name);
4634 if (FAILED(hr))
4635 continue;
4637 memset(&ddsd, 0, sizeof(ddsd));
4638 ddsd.dwSize = sizeof(ddsd);
4639 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
4640 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", j, hr);
4641 ok(ddsd.dwFlags == (DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_LINEARSIZE),
4642 "Test %u: Got unexpected flags %#x.\n", j, ddsd.dwFlags);
4643 if (user_mem_tests[j].flags & DDSD_LPSURFACE)
4644 ok(U1(ddsd).dwLinearSize == ~0u, "Test %u: Got unexpected linear size %#x.\n",
4645 j, U1(ddsd).dwLinearSize);
4646 else
4647 ok(U1(ddsd).dwLinearSize == 2 * 2 * formats[i].block_size,
4648 "Test %u: Got unexpected linear size %#x, expected %#x.\n",
4649 j, U1(ddsd).dwLinearSize, 2 * 2 * formats[i].block_size);
4650 IDirectDrawSurface4_Release(surface);
4654 HeapFree(GetProcessHeap(), 0, mem);
4655 cleanup:
4656 IDirectDraw4_Release(ddraw);
4657 IDirect3DDevice3_Release(device);
4658 DestroyWindow(window);
4661 struct format_support_check
4663 const DDPIXELFORMAT *format;
4664 BOOL supported;
4667 static HRESULT WINAPI test_unsupported_formats_cb(DDPIXELFORMAT *fmt, void *ctx)
4669 struct format_support_check *format = ctx;
4671 if (!memcmp(format->format, fmt, sizeof(*fmt)))
4673 format->supported = TRUE;
4674 return DDENUMRET_CANCEL;
4677 return DDENUMRET_OK;
4680 static void test_unsupported_formats(void)
4682 HRESULT hr;
4683 BOOL expect_success;
4684 HWND window;
4685 IDirectDraw4 *ddraw;
4686 IDirect3D3 *d3d;
4687 IDirect3DDevice3 *device;
4688 IDirectDrawSurface4 *surface;
4689 DDSURFACEDESC2 ddsd;
4690 unsigned int i, j;
4691 DWORD expected_caps;
4692 static const struct
4694 const char *name;
4695 DDPIXELFORMAT fmt;
4697 formats[] =
4700 "D3DFMT_A8R8G8B8",
4702 sizeof(DDPIXELFORMAT), DDPF_RGB | DDPF_ALPHAPIXELS, 0,
4703 {32}, {0x00ff0000}, {0x0000ff00}, {0x000000ff}, {0xff000000}
4707 "D3DFMT_P8",
4709 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
4710 {8 }, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}
4714 static const DWORD caps[] = {0, DDSCAPS_SYSTEMMEMORY, DDSCAPS_VIDEOMEMORY};
4716 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4717 0, 0, 640, 480, 0, 0, 0, 0);
4719 if (!(device = create_device(window, DDSCL_NORMAL)))
4721 skip("Failed to create a 3D device, skipping test.\n");
4722 DestroyWindow(window);
4723 return;
4726 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
4727 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
4728 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **) &ddraw);
4729 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
4730 IDirect3D3_Release(d3d);
4732 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
4734 struct format_support_check check = {&formats[i].fmt, FALSE};
4735 hr = IDirect3DDevice3_EnumTextureFormats(device, test_unsupported_formats_cb, &check);
4736 ok(SUCCEEDED(hr), "Failed to enumerate texture formats %#x.\n", hr);
4738 for (j = 0; j < sizeof(caps) / sizeof(*caps); j++)
4740 memset(&ddsd, 0, sizeof(ddsd));
4741 ddsd.dwSize = sizeof(ddsd);
4742 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
4743 U4(ddsd).ddpfPixelFormat = formats[i].fmt;
4744 ddsd.dwWidth = 4;
4745 ddsd.dwHeight = 4;
4746 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | caps[j];
4748 if (caps[j] & DDSCAPS_VIDEOMEMORY && !check.supported)
4749 expect_success = FALSE;
4750 else
4751 expect_success = TRUE;
4753 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
4754 ok(SUCCEEDED(hr) == expect_success,
4755 "Got unexpected hr %#x for format %s, caps %#x, expected %s.\n",
4756 hr, formats[i].name, caps[j], expect_success ? "success" : "failure");
4757 if (FAILED(hr))
4758 continue;
4760 memset(&ddsd, 0, sizeof(ddsd));
4761 ddsd.dwSize = sizeof(ddsd);
4762 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
4763 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
4765 if (caps[j] & DDSCAPS_VIDEOMEMORY)
4766 expected_caps = DDSCAPS_VIDEOMEMORY;
4767 else if (caps[j] & DDSCAPS_SYSTEMMEMORY)
4768 expected_caps = DDSCAPS_SYSTEMMEMORY;
4769 else if (check.supported)
4770 expected_caps = DDSCAPS_VIDEOMEMORY;
4771 else
4772 expected_caps = DDSCAPS_SYSTEMMEMORY;
4774 ok(ddsd.ddsCaps.dwCaps & expected_caps,
4775 "Expected capability %#x, format %s, input cap %#x.\n",
4776 expected_caps, formats[i].name, caps[j]);
4778 IDirectDrawSurface4_Release(surface);
4782 IDirectDraw4_Release(ddraw);
4783 IDirect3DDevice3_Release(device);
4784 DestroyWindow(window);
4787 static void test_rt_caps(void)
4789 PALETTEENTRY palette_entries[256];
4790 IDirectDrawPalette *palette;
4791 IDirectDraw4 *ddraw;
4792 DDPIXELFORMAT z_fmt;
4793 IDirect3D3 *d3d;
4794 unsigned int i;
4795 ULONG refcount;
4796 HWND window;
4797 HRESULT hr;
4799 static const DDPIXELFORMAT p8_fmt =
4801 sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8 | DDPF_RGB, 0,
4802 {8}, {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000},
4805 const struct
4807 const DDPIXELFORMAT *pf;
4808 DWORD caps_in;
4809 DWORD caps_out;
4810 HRESULT create_device_hr;
4811 HRESULT set_rt_hr, alternative_set_rt_hr;
4813 test_data[] =
4816 NULL,
4817 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
4818 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4819 D3D_OK,
4820 D3D_OK,
4821 D3D_OK,
4824 NULL,
4825 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4826 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4827 D3D_OK,
4828 D3D_OK,
4829 D3D_OK,
4832 NULL,
4833 DDSCAPS_OFFSCREENPLAIN,
4834 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4835 DDERR_INVALIDCAPS,
4836 DDERR_INVALIDCAPS,
4837 DDERR_INVALIDCAPS,
4840 NULL,
4841 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4842 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4843 D3DERR_SURFACENOTINVIDMEM,
4844 D3D_OK,
4845 D3D_OK,
4848 NULL,
4849 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4850 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4851 DDERR_INVALIDCAPS,
4852 DDERR_INVALIDCAPS,
4853 DDERR_INVALIDCAPS,
4856 NULL,
4857 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY,
4858 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4859 D3D_OK,
4860 D3D_OK,
4861 D3D_OK,
4864 NULL,
4865 DDSCAPS_3DDEVICE,
4866 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4867 D3D_OK,
4868 D3D_OK,
4869 D3D_OK,
4872 NULL,
4874 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4875 DDERR_INVALIDCAPS,
4876 DDERR_INVALIDCAPS,
4877 DDERR_INVALIDCAPS,
4880 NULL,
4881 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4882 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4883 D3DERR_SURFACENOTINVIDMEM,
4884 D3D_OK,
4885 D3D_OK,
4888 NULL,
4889 DDSCAPS_SYSTEMMEMORY,
4890 DDSCAPS_SYSTEMMEMORY,
4891 DDERR_INVALIDCAPS,
4892 DDERR_INVALIDCAPS,
4893 DDERR_INVALIDCAPS,
4896 &p8_fmt,
4898 DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4899 DDERR_INVALIDCAPS,
4900 DDERR_INVALIDCAPS,
4901 DDERR_INVALIDCAPS,
4904 &p8_fmt,
4905 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
4906 ~0U /* AMD r200 */,
4907 DDERR_NOPALETTEATTACHED,
4908 DDERR_INVALIDCAPS,
4909 DDERR_INVALIDCAPS,
4912 &p8_fmt,
4913 DDSCAPS_OFFSCREENPLAIN,
4914 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM,
4915 DDERR_INVALIDCAPS,
4916 DDERR_INVALIDCAPS,
4917 DDERR_INVALIDCAPS,
4920 &p8_fmt,
4921 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4922 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE,
4923 DDERR_NOPALETTEATTACHED,
4924 DDERR_INVALIDCAPS,
4925 DDERR_INVALIDCAPS,
4928 &p8_fmt,
4929 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4930 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
4931 DDERR_INVALIDCAPS,
4932 DDERR_INVALIDCAPS,
4933 DDERR_INVALIDCAPS,
4936 &z_fmt,
4937 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER,
4938 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4939 DDERR_INVALIDCAPS,
4940 DDERR_INVALIDPIXELFORMAT,
4941 D3D_OK /* r200 */,
4944 &z_fmt,
4945 DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4946 DDSCAPS_3DDEVICE | DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4947 DDERR_INVALIDCAPS,
4948 DDERR_INVALIDPIXELFORMAT,
4949 D3D_OK /* r200 */,
4952 &z_fmt,
4953 DDSCAPS_ZBUFFER,
4954 DDSCAPS_VIDEOMEMORY | DDSCAPS_ZBUFFER | DDSCAPS_LOCALVIDMEM,
4955 DDERR_INVALIDCAPS,
4956 DDERR_INVALIDCAPS,
4957 DDERR_INVALIDCAPS,
4960 &z_fmt,
4961 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4962 DDSCAPS_SYSTEMMEMORY | DDSCAPS_3DDEVICE | DDSCAPS_ZBUFFER,
4963 DDERR_INVALIDCAPS,
4964 DDERR_INVALIDPIXELFORMAT,
4965 D3D_OK /* r200 */,
4968 &z_fmt,
4969 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
4970 DDSCAPS_SYSTEMMEMORY | DDSCAPS_ZBUFFER,
4971 DDERR_INVALIDCAPS,
4972 DDERR_INVALIDCAPS,
4973 DDERR_INVALIDCAPS,
4977 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
4978 0, 0, 640, 480, 0, 0, 0, 0);
4979 ddraw = create_ddraw();
4980 ok(!!ddraw, "Failed to create a ddraw object.\n");
4981 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
4982 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
4984 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
4986 skip("D3D interface is not available, skipping test.\n");
4987 goto done;
4990 memset(&z_fmt, 0, sizeof(z_fmt));
4991 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
4992 if (FAILED(hr) || !z_fmt.dwSize)
4994 skip("No depth buffer formats available, skipping test.\n");
4995 IDirect3D3_Release(d3d);
4996 goto done;
4999 memset(palette_entries, 0, sizeof(palette_entries));
5000 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
5001 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
5003 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5005 IDirectDrawSurface4 *surface, *rt, *expected_rt, *tmp;
5006 DDSURFACEDESC2 surface_desc;
5007 IDirect3DDevice3 *device;
5009 memset(&surface_desc, 0, sizeof(surface_desc));
5010 surface_desc.dwSize = sizeof(surface_desc);
5011 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5012 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5013 if (test_data[i].pf)
5015 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5016 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
5018 surface_desc.dwWidth = 640;
5019 surface_desc.dwHeight = 480;
5020 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5021 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
5022 i, test_data[i].caps_in, hr);
5024 memset(&surface_desc, 0, sizeof(surface_desc));
5025 surface_desc.dwSize = sizeof(surface_desc);
5026 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
5027 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5028 ok(test_data[i].caps_out == ~0U || surface_desc.ddsCaps.dwCaps == test_data[i].caps_out,
5029 "Test %u: Got unexpected caps %#x, expected %#x.\n",
5030 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
5032 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5033 ok(hr == test_data[i].create_device_hr, "Test %u: Got unexpected hr %#x, expected %#x.\n",
5034 i, hr, test_data[i].create_device_hr);
5035 if (FAILED(hr))
5037 if (hr == DDERR_NOPALETTEATTACHED)
5039 hr = IDirectDrawSurface4_SetPalette(surface, palette);
5040 ok(SUCCEEDED(hr), "Test %u: Failed to set palette, hr %#x.\n", i, hr);
5041 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5042 if (surface_desc.ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
5043 ok(hr == DDERR_INVALIDPIXELFORMAT, "Test %u: Got unexpected hr %#x.\n", i, hr);
5044 else
5045 ok(hr == D3DERR_SURFACENOTINVIDMEM, "Test %u: Got unexpected hr %#x.\n", i, hr);
5047 IDirectDrawSurface4_Release(surface);
5049 memset(&surface_desc, 0, sizeof(surface_desc));
5050 surface_desc.dwSize = sizeof(surface_desc);
5051 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5052 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
5053 surface_desc.dwWidth = 640;
5054 surface_desc.dwHeight = 480;
5055 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5056 ok(SUCCEEDED(hr), "Test %u: Failed to create surface, hr %#x.\n", i, hr);
5058 hr = IDirect3D3_CreateDevice(d3d, &IID_IDirect3DHALDevice, surface, &device, NULL);
5059 ok(SUCCEEDED(hr), "Test %u: Failed to create device, hr %#x.\n", i, hr);
5062 memset(&surface_desc, 0, sizeof(surface_desc));
5063 surface_desc.dwSize = sizeof(surface_desc);
5064 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5065 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5066 if (test_data[i].pf)
5068 surface_desc.dwFlags |= DDSD_PIXELFORMAT;
5069 U4(surface_desc).ddpfPixelFormat = *test_data[i].pf;
5071 surface_desc.dwWidth = 640;
5072 surface_desc.dwHeight = 480;
5073 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &rt, NULL);
5074 ok(SUCCEEDED(hr), "Test %u: Failed to create surface with caps %#x, hr %#x.\n",
5075 i, test_data[i].caps_in, hr);
5077 hr = IDirect3DDevice3_SetRenderTarget(device, rt, 0);
5078 ok(hr == test_data[i].set_rt_hr || broken(hr == test_data[i].alternative_set_rt_hr),
5079 "Test %u: Got unexpected hr %#x, expected %#x.\n",
5080 i, hr, test_data[i].set_rt_hr);
5081 if (SUCCEEDED(hr) || hr == DDERR_INVALIDPIXELFORMAT)
5082 expected_rt = rt;
5083 else
5084 expected_rt = surface;
5086 hr = IDirect3DDevice3_GetRenderTarget(device, &tmp);
5087 ok(SUCCEEDED(hr), "Test %u: Failed to get render target, hr %#x.\n", i, hr);
5088 ok(tmp == expected_rt, "Test %u: Got unexpected rt %p.\n", i, tmp);
5090 IDirectDrawSurface4_Release(tmp);
5091 IDirectDrawSurface4_Release(rt);
5092 refcount = IDirect3DDevice3_Release(device);
5093 ok(refcount == 0, "Test %u: The device was not properly freed, refcount %u.\n", i, refcount);
5094 refcount = IDirectDrawSurface4_Release(surface);
5095 ok(refcount == 0, "Test %u: The surface was not properly freed, refcount %u.\n", i, refcount);
5098 IDirectDrawPalette_Release(palette);
5099 IDirect3D3_Release(d3d);
5101 done:
5102 refcount = IDirectDraw4_Release(ddraw);
5103 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5104 DestroyWindow(window);
5107 static void test_primary_caps(void)
5109 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
5110 IDirectDrawSurface4 *surface;
5111 DDSURFACEDESC2 surface_desc;
5112 IDirectDraw4 *ddraw;
5113 unsigned int i;
5114 ULONG refcount;
5115 HWND window;
5116 HRESULT hr;
5118 static const struct
5120 DWORD coop_level;
5121 DWORD caps_in;
5122 DWORD back_buffer_count;
5123 HRESULT hr;
5124 DWORD caps_out;
5126 test_data[] =
5129 DDSCL_NORMAL,
5130 DDSCAPS_PRIMARYSURFACE,
5131 ~0u,
5132 DD_OK,
5133 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE,
5136 DDSCL_NORMAL,
5137 DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE,
5138 ~0u,
5139 DDERR_INVALIDCAPS,
5140 ~0u,
5143 DDSCL_NORMAL,
5144 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER,
5145 ~0u,
5146 DDERR_INVALIDCAPS,
5147 ~0u,
5150 DDSCL_NORMAL,
5151 DDSCAPS_PRIMARYSURFACE | DDSCAPS_BACKBUFFER,
5152 ~0u,
5153 DDERR_INVALIDCAPS,
5154 ~0u,
5157 DDSCL_NORMAL,
5158 DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP,
5159 ~0u,
5160 DDERR_INVALIDCAPS,
5161 ~0u,
5164 DDSCL_NORMAL,
5165 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX,
5166 ~0u,
5167 DDERR_INVALIDCAPS,
5168 ~0u,
5171 DDSCL_NORMAL,
5172 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5173 ~0u,
5174 DDERR_INVALIDCAPS,
5175 ~0u,
5178 DDSCL_NORMAL,
5179 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5181 DDERR_INVALIDCAPS,
5182 ~0u,
5185 DDSCL_NORMAL,
5186 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5188 DDERR_NOEXCLUSIVEMODE,
5189 ~0u,
5192 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5193 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5195 DDERR_INVALIDCAPS,
5196 ~0u,
5199 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5200 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP,
5202 DD_OK,
5203 DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX,
5206 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5207 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER,
5209 DDERR_INVALIDCAPS,
5210 ~0u,
5213 DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN,
5214 DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_BACKBUFFER,
5216 DDERR_INVALIDCAPS,
5217 ~0u,
5221 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5222 0, 0, 640, 480, 0, 0, 0, 0);
5223 ddraw = create_ddraw();
5224 ok(!!ddraw, "Failed to create a ddraw object.\n");
5226 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
5228 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, test_data[i].coop_level);
5229 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5231 memset(&surface_desc, 0, sizeof(surface_desc));
5232 surface_desc.dwSize = sizeof(surface_desc);
5233 surface_desc.dwFlags = DDSD_CAPS;
5234 if (test_data[i].back_buffer_count != ~0u)
5235 surface_desc.dwFlags |= DDSD_BACKBUFFERCOUNT;
5236 surface_desc.ddsCaps.dwCaps = test_data[i].caps_in;
5237 U5(surface_desc).dwBackBufferCount = test_data[i].back_buffer_count;
5238 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5239 ok(hr == test_data[i].hr, "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
5240 if (FAILED(hr))
5241 continue;
5243 memset(&surface_desc, 0, sizeof(surface_desc));
5244 surface_desc.dwSize = sizeof(surface_desc);
5245 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
5246 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
5247 ok((surface_desc.ddsCaps.dwCaps & ~placement) == test_data[i].caps_out,
5248 "Test %u: Got unexpected caps %#x, expected %#x.\n",
5249 i, surface_desc.ddsCaps.dwCaps, test_data[i].caps_out);
5251 IDirectDrawSurface4_Release(surface);
5254 refcount = IDirectDraw4_Release(ddraw);
5255 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5256 DestroyWindow(window);
5259 static void test_surface_lock(void)
5261 IDirectDraw4 *ddraw;
5262 IDirect3D3 *d3d = NULL;
5263 IDirectDrawSurface4 *surface;
5264 HRESULT hr;
5265 HWND window;
5266 unsigned int i;
5267 DDSURFACEDESC2 ddsd;
5268 ULONG refcount;
5269 DDPIXELFORMAT z_fmt;
5270 static const struct
5272 DWORD caps;
5273 DWORD caps2;
5274 const char *name;
5276 tests[] =
5279 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY,
5281 "videomemory offscreenplain"
5284 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY,
5286 "systemmemory offscreenplain"
5289 DDSCAPS_PRIMARYSURFACE,
5291 "primary"
5294 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
5296 "videomemory texture"
5299 DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY,
5300 DDSCAPS2_OPAQUE,
5301 "opaque videomemory texture"
5304 DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY,
5306 "systemmemory texture"
5309 DDSCAPS_TEXTURE,
5310 DDSCAPS2_TEXTUREMANAGE,
5311 "managed texture"
5314 DDSCAPS_TEXTURE,
5315 DDSCAPS2_D3DTEXTUREMANAGE,
5316 "managed texture"
5319 DDSCAPS_TEXTURE,
5320 DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_OPAQUE,
5321 "opaque managed texture"
5324 DDSCAPS_TEXTURE,
5325 DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_OPAQUE,
5326 "opaque managed texture"
5329 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE,
5331 "render target"
5334 DDSCAPS_ZBUFFER,
5336 "Z buffer"
5340 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5341 0, 0, 640, 480, 0, 0, 0, 0);
5342 ddraw = create_ddraw();
5343 ok(!!ddraw, "Failed to create a ddraw object.\n");
5344 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5345 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5347 if (FAILED(IDirectDraw4_QueryInterface(ddraw, &IID_IDirect3D3, (void **)&d3d)))
5349 skip("D3D interface is not available, skipping test.\n");
5350 goto done;
5353 memset(&z_fmt, 0, sizeof(z_fmt));
5354 hr = IDirect3D3_EnumZBufferFormats(d3d, &IID_IDirect3DHALDevice, enum_z_fmt, &z_fmt);
5355 if (FAILED(hr) || !z_fmt.dwSize)
5357 skip("No depth buffer formats available, skipping test.\n");
5358 goto done;
5361 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
5363 memset(&ddsd, 0, sizeof(ddsd));
5364 ddsd.dwSize = sizeof(ddsd);
5365 ddsd.dwFlags = DDSD_CAPS;
5366 if (!(tests[i].caps & DDSCAPS_PRIMARYSURFACE))
5368 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
5369 ddsd.dwWidth = 64;
5370 ddsd.dwHeight = 64;
5372 if (tests[i].caps & DDSCAPS_ZBUFFER)
5374 ddsd.dwFlags |= DDSD_PIXELFORMAT;
5375 U4(ddsd).ddpfPixelFormat = z_fmt;
5377 ddsd.ddsCaps.dwCaps = tests[i].caps;
5378 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
5380 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5381 ok(SUCCEEDED(hr), "Failed to create surface, type %s, hr %#x.\n", tests[i].name, hr);
5383 memset(&ddsd, 0, sizeof(ddsd));
5384 ddsd.dwSize = sizeof(ddsd);
5385 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_WAIT, NULL);
5386 ok(SUCCEEDED(hr), "Failed to lock surface, type %s, hr %#x.\n", tests[i].name, hr);
5387 if (SUCCEEDED(hr))
5389 hr = IDirectDrawSurface4_Unlock(surface, NULL);
5390 ok(SUCCEEDED(hr), "Failed to unlock surface, type %s, hr %#x.\n", tests[i].name, hr);
5393 IDirectDrawSurface4_Release(surface);
5396 done:
5397 if (d3d)
5398 IDirect3D3_Release(d3d);
5399 refcount = IDirectDraw4_Release(ddraw);
5400 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5401 DestroyWindow(window);
5404 static void test_surface_discard(void)
5406 IDirect3DDevice3 *device;
5407 IDirect3D3 *d3d;
5408 IDirectDraw4 *ddraw;
5409 HRESULT hr;
5410 HWND window;
5411 DDSURFACEDESC2 ddsd;
5412 IDirectDrawSurface4 *surface, *target;
5413 void *addr;
5414 static const struct
5416 DWORD caps, caps2;
5417 BOOL discard;
5419 tests[] =
5421 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY, 0, TRUE},
5422 {DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
5423 {DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY, 0, TRUE},
5424 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, FALSE},
5425 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE},
5426 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
5427 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE},
5428 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE | DDSCAPS2_HINTDYNAMIC, FALSE},
5430 unsigned int i;
5432 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5433 0, 0, 640, 480, 0, 0, 0, 0);
5435 if (!(device = create_device(window, DDSCL_NORMAL)))
5437 skip("Failed to create a 3D device, skipping test.\n");
5438 DestroyWindow(window);
5439 return;
5441 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
5442 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
5443 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
5444 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
5445 hr = IDirect3DDevice3_GetRenderTarget(device, &target);
5446 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
5448 for (i = 0; i < sizeof(tests) / sizeof(*tests); i++)
5450 BOOL discarded;
5452 memset(&ddsd, 0, sizeof(ddsd));
5453 ddsd.dwSize = sizeof(ddsd);
5454 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5455 ddsd.ddsCaps.dwCaps = tests[i].caps;
5456 ddsd.ddsCaps.dwCaps2 = tests[i].caps2;
5457 ddsd.dwWidth = 64;
5458 ddsd.dwHeight = 64;
5459 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5460 ok(SUCCEEDED(hr), "Failed to create offscreen surface, hr %#x, case %u.\n", hr, i);
5462 memset(&ddsd, 0, sizeof(ddsd));
5463 ddsd.dwSize = sizeof(ddsd);
5464 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, 0, NULL);
5465 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5466 addr = ddsd.lpSurface;
5467 hr = IDirectDrawSurface4_Unlock(surface, NULL);
5468 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5470 memset(&ddsd, 0, sizeof(ddsd));
5471 ddsd.dwSize = sizeof(ddsd);
5472 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
5473 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5474 discarded = ddsd.lpSurface != addr;
5475 hr = IDirectDrawSurface4_Unlock(surface, NULL);
5476 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5478 hr = IDirectDrawSurface4_Blt(target, NULL, surface, NULL, DDBLT_WAIT, NULL);
5479 ok(SUCCEEDED(hr), "Failed to blit, hr %#x.\n", hr);
5481 memset(&ddsd, 0, sizeof(ddsd));
5482 ddsd.dwSize = sizeof(ddsd);
5483 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, DDLOCK_DISCARDCONTENTS, NULL);
5484 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5485 discarded |= ddsd.lpSurface != addr;
5486 hr = IDirectDrawSurface4_Unlock(surface, NULL);
5487 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5489 IDirectDrawSurface4_Release(surface);
5491 /* Windows 7 reliably changes the address of surfaces that are discardable (Nvidia Kepler,
5492 * AMD r500, evergreen). Windows XP, at least on AMD r200, does not. */
5493 ok(!discarded || tests[i].discard, "Expected surface not to be discarded, case %u\n", i);
5496 IDirectDrawSurface4_Release(target);
5497 IDirectDraw4_Release(ddraw);
5498 IDirect3D3_Release(d3d);
5499 IDirect3DDevice3_Release(device);
5500 DestroyWindow(window);
5503 static void test_flip(void)
5505 const DWORD placement = DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
5506 IDirectDrawSurface4 *primary, *backbuffer1, *backbuffer2, *backbuffer3, *surface;
5507 DDSCAPS2 caps = {DDSCAPS_FLIP, 0, 0, {0}};
5508 DDSURFACEDESC2 surface_desc;
5509 BOOL sysmem_primary;
5510 IDirectDraw4 *ddraw;
5511 D3DCOLOR color;
5512 ULONG refcount;
5513 HWND window;
5514 DDBLTFX fx;
5515 HRESULT hr;
5517 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5518 0, 0, 640, 480, 0, 0, 0, 0);
5519 ddraw = create_ddraw();
5520 ok(!!ddraw, "Failed to create a ddraw object.\n");
5522 hr = set_display_mode(ddraw, 640, 480);
5523 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
5524 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
5525 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5527 memset(&surface_desc, 0, sizeof(surface_desc));
5528 surface_desc.dwSize = sizeof(surface_desc);
5529 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
5530 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
5531 U5(surface_desc).dwBackBufferCount = 3;
5532 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
5533 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5535 memset(&surface_desc, 0, sizeof(surface_desc));
5536 surface_desc.dwSize = sizeof(surface_desc);
5537 hr = IDirectDrawSurface4_GetSurfaceDesc(primary, &surface_desc);
5538 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5539 ok((surface_desc.ddsCaps.dwCaps & ~placement)
5540 == (DDSCAPS_VISIBLE | DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER | DDSCAPS_FLIP | DDSCAPS_COMPLEX),
5541 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5542 sysmem_primary = surface_desc.ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY;
5544 hr = IDirectDrawSurface4_GetAttachedSurface(primary, &caps, &backbuffer1);
5545 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5546 memset(&surface_desc, 0, sizeof(surface_desc));
5547 surface_desc.dwSize = sizeof(surface_desc);
5548 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer1, &surface_desc);
5549 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5550 ok(!U5(surface_desc).dwBackBufferCount, "Got unexpected back buffer count %u.\n", U5(surface_desc).dwBackBufferCount);
5551 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_BACKBUFFER),
5552 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5554 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer1, &caps, &backbuffer2);
5555 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5556 memset(&surface_desc, 0, sizeof(surface_desc));
5557 surface_desc.dwSize = sizeof(surface_desc);
5558 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer2, &surface_desc);
5559 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5560 ok(!U5(surface_desc).dwBackBufferCount, "Got unexpected back buffer count %u.\n", U5(surface_desc).dwBackBufferCount);
5561 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
5562 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5564 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer2, &caps, &backbuffer3);
5565 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5566 memset(&surface_desc, 0, sizeof(surface_desc));
5567 surface_desc.dwSize = sizeof(surface_desc);
5568 hr = IDirectDrawSurface4_GetSurfaceDesc(backbuffer3, &surface_desc);
5569 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5570 ok(!U5(surface_desc).dwBackBufferCount, "Got unexpected back buffer count %u.\n", U5(surface_desc).dwBackBufferCount);
5571 ok((surface_desc.ddsCaps.dwCaps & ~placement) == (DDSCAPS_FLIP | DDSCAPS_COMPLEX),
5572 "Got unexpected caps %#x.\n", surface_desc.ddsCaps.dwCaps);
5574 hr = IDirectDrawSurface4_GetAttachedSurface(backbuffer3, &caps, &surface);
5575 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
5576 ok(surface == primary, "Got unexpected surface %p, expected %p.\n", surface, primary);
5577 IDirectDrawSurface4_Release(surface);
5579 memset(&surface_desc, 0, sizeof(surface_desc));
5580 surface_desc.dwSize = sizeof(surface_desc);
5581 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
5582 surface_desc.ddsCaps.dwCaps = 0;
5583 surface_desc.dwWidth = 640;
5584 surface_desc.dwHeight = 480;
5585 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
5586 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5587 hr = IDirectDrawSurface4_Flip(primary, surface, DDFLIP_WAIT);
5588 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5589 IDirectDrawSurface4_Release(surface);
5591 hr = IDirectDrawSurface4_Flip(primary, primary, DDFLIP_WAIT);
5592 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5593 hr = IDirectDrawSurface4_Flip(backbuffer1, NULL, DDFLIP_WAIT);
5594 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5595 hr = IDirectDrawSurface4_Flip(backbuffer2, NULL, DDFLIP_WAIT);
5596 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5597 hr = IDirectDrawSurface4_Flip(backbuffer3, NULL, DDFLIP_WAIT);
5598 ok(hr == DDERR_NOTFLIPPABLE, "Got unexpected hr %#x.\n", hr);
5600 memset(&fx, 0, sizeof(fx));
5601 fx.dwSize = sizeof(fx);
5602 U5(fx).dwFillColor = 0xffff0000;
5603 hr = IDirectDrawSurface4_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5604 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5605 U5(fx).dwFillColor = 0xff00ff00;
5606 hr = IDirectDrawSurface4_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5607 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5608 U5(fx).dwFillColor = 0xff0000ff;
5609 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5610 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5612 hr = IDirectDrawSurface4_Flip(primary, NULL, DDFLIP_WAIT);
5613 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5614 color = get_surface_color(backbuffer1, 320, 240);
5615 /* The testbot seems to just copy the contents of one surface to all the
5616 * others, instead of properly flipping. */
5617 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
5618 "Got unexpected color 0x%08x.\n", color);
5619 color = get_surface_color(backbuffer2, 320, 240);
5620 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
5621 U5(fx).dwFillColor = 0xffff0000;
5622 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5623 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5625 hr = IDirectDrawSurface4_Flip(primary, NULL, DDFLIP_WAIT);
5626 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5627 color = get_surface_color(backbuffer1, 320, 240);
5628 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
5629 "Got unexpected color 0x%08x.\n", color);
5630 color = get_surface_color(backbuffer2, 320, 240);
5631 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5632 U5(fx).dwFillColor = 0xff00ff00;
5633 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5634 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5636 hr = IDirectDrawSurface4_Flip(primary, NULL, DDFLIP_WAIT);
5637 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5638 color = get_surface_color(backbuffer1, 320, 240);
5639 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
5640 "Got unexpected color 0x%08x.\n", color);
5641 color = get_surface_color(backbuffer2, 320, 240);
5642 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
5643 U5(fx).dwFillColor = 0xff0000ff;
5644 hr = IDirectDrawSurface4_Blt(backbuffer3, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5645 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5647 hr = IDirectDrawSurface4_Flip(primary, backbuffer1, DDFLIP_WAIT);
5648 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5649 color = get_surface_color(backbuffer2, 320, 240);
5650 ok(compare_color(color, 0x0000ff00, 1) || broken(sysmem_primary && compare_color(color, 0x000000ff, 1)),
5651 "Got unexpected color 0x%08x.\n", color);
5652 color = get_surface_color(backbuffer3, 320, 240);
5653 ok(compare_color(color, 0x000000ff, 1), "Got unexpected color 0x%08x.\n", color);
5654 U5(fx).dwFillColor = 0xffff0000;
5655 hr = IDirectDrawSurface4_Blt(backbuffer1, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5656 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5658 hr = IDirectDrawSurface4_Flip(primary, backbuffer2, DDFLIP_WAIT);
5659 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5660 color = get_surface_color(backbuffer1, 320, 240);
5661 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
5662 color = get_surface_color(backbuffer3, 320, 240);
5663 ok(compare_color(color, 0x000000ff, 1) || broken(sysmem_primary && compare_color(color, 0x00ff0000, 1)),
5664 "Got unexpected color 0x%08x.\n", color);
5665 U5(fx).dwFillColor = 0xff00ff00;
5666 hr = IDirectDrawSurface4_Blt(backbuffer2, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
5667 ok(SUCCEEDED(hr), "Failed to fill surface, hr %#x.\n", hr);
5669 hr = IDirectDrawSurface4_Flip(primary, backbuffer3, DDFLIP_WAIT);
5670 ok(SUCCEEDED(hr), "Failed to flip, hr %#x.\n", hr);
5671 color = get_surface_color(backbuffer1, 320, 240);
5672 ok(compare_color(color, 0x00ff0000, 1) || broken(sysmem_primary && compare_color(color, 0x0000ff00, 1)),
5673 "Got unexpected color 0x%08x.\n", color);
5674 color = get_surface_color(backbuffer2, 320, 240);
5675 ok(compare_color(color, 0x0000ff00, 1), "Got unexpected color 0x%08x.\n", color);
5677 IDirectDrawSurface4_Release(backbuffer3);
5678 IDirectDrawSurface4_Release(backbuffer2);
5679 IDirectDrawSurface4_Release(backbuffer1);
5680 IDirectDrawSurface4_Release(primary);
5681 refcount = IDirectDraw4_Release(ddraw);
5682 ok(refcount == 0, "The ddraw object was not properly freed, refcount %u.\n", refcount);
5683 DestroyWindow(window);
5686 static void reset_ddsd(DDSURFACEDESC2 *ddsd)
5688 memset(ddsd, 0, sizeof(*ddsd));
5689 ddsd->dwSize = sizeof(*ddsd);
5692 static void test_set_surface_desc(void)
5694 IDirectDraw4 *ddraw;
5695 HWND window;
5696 HRESULT hr;
5697 DDSURFACEDESC2 ddsd;
5698 IDirectDrawSurface4 *surface;
5699 BYTE data[16*16*4];
5700 ULONG ref;
5701 unsigned int i;
5702 static const struct
5704 DWORD caps, caps2;
5705 BOOL supported;
5706 const char *name;
5708 invalid_caps_tests[] =
5710 {DDSCAPS_VIDEOMEMORY, 0, FALSE, "videomemory plain"},
5711 {DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY, 0, TRUE, "systemmemory texture"},
5712 {DDSCAPS_TEXTURE, DDSCAPS2_D3DTEXTUREMANAGE, FALSE, "managed texture"},
5713 {DDSCAPS_TEXTURE, DDSCAPS2_TEXTUREMANAGE, FALSE, "managed texture"},
5714 {DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY, 0, FALSE, "systemmemory primary"},
5717 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
5718 0, 0, 640, 480, 0, 0, 0, 0);
5719 ddraw = create_ddraw();
5720 ok(!!ddraw, "Failed to create a ddraw object.\n");
5721 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
5722 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
5724 reset_ddsd(&ddsd);
5725 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
5726 ddsd.dwWidth = 8;
5727 ddsd.dwHeight = 8;
5728 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5729 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5730 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5731 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5732 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5733 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5734 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5736 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5737 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
5739 reset_ddsd(&ddsd);
5740 ddsd.dwFlags = DDSD_LPSURFACE;
5741 ddsd.lpSurface = data;
5742 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5743 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5745 /* Redundantly setting the same lpSurface is not an error. */
5746 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5747 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5748 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5749 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5750 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
5751 ok(ddsd.lpSurface == NULL, "lpSurface is %p, expected NULL.\n", ddsd.lpSurface);
5753 hr = IDirectDrawSurface4_Lock(surface, NULL, &ddsd, 0, NULL);
5754 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
5755 ok(!(ddsd.dwFlags & DDSD_LPSURFACE), "DDSD_LPSURFACE is set.\n");
5756 ok(ddsd.lpSurface == data, "lpSurface is %p, expected %p.\n", data, data);
5757 hr = IDirectDrawSurface4_Unlock(surface, NULL);
5758 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
5760 reset_ddsd(&ddsd);
5761 ddsd.dwFlags = DDSD_LPSURFACE;
5762 ddsd.lpSurface = data;
5763 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 1);
5764 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with flags=1 returned %#x.\n", hr);
5766 ddsd.lpSurface = NULL;
5767 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5768 ok(hr == DDERR_INVALIDPARAMS, "Setting lpSurface=NULL returned %#x.\n", hr);
5770 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, NULL, 0);
5771 ok(hr == DDERR_INVALIDPARAMS, "SetSurfaceDesc with NULL desc returned %#x.\n", hr);
5773 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5774 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5775 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
5776 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
5777 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
5779 /* Setting the caps is an error. This also means the original description cannot be reapplied. */
5780 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5781 ok(hr == DDERR_INVALIDPARAMS, "Setting the original desc returned %#x.\n", hr);
5783 ddsd.dwFlags = DDSD_CAPS;
5784 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5785 ok(hr == DDERR_INVALIDPARAMS, "Setting DDSD_CAPS returned %#x.\n", hr);
5787 /* dwCaps = 0 is allowed, but ignored. Caps2 can be anything and is ignored too. */
5788 ddsd.dwFlags = DDSD_CAPS | DDSD_LPSURFACE;
5789 ddsd.lpSurface = data;
5790 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5791 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
5792 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
5793 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5794 ok(hr == DDERR_INVALIDCAPS, "Setting DDSD_CAPS returned %#x.\n", hr);
5795 ddsd.ddsCaps.dwCaps = 0;
5796 ddsd.ddsCaps.dwCaps2 = 0xdeadbeef;
5797 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5798 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5800 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5801 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5802 ok(ddsd.ddsCaps.dwCaps == (DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN),
5803 "Got unexpected caps %#x.\n", ddsd.ddsCaps.dwCaps);
5804 ok(ddsd.ddsCaps.dwCaps2 == 0, "Got unexpected caps2 %#x.\n", 0);
5806 /* Setting the height is allowed, but it cannot be set to 0, and only if LPSURFACE is set too. */
5807 reset_ddsd(&ddsd);
5808 ddsd.dwFlags = DDSD_HEIGHT;
5809 ddsd.dwHeight = 16;
5810 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5811 ok(hr == DDERR_INVALIDPARAMS, "Setting height without lpSurface returned %#x.\n", hr);
5813 ddsd.lpSurface = data;
5814 ddsd.dwFlags = DDSD_HEIGHT | DDSD_LPSURFACE;
5815 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5816 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5818 ddsd.dwHeight = 0;
5819 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5820 ok(hr == DDERR_INVALIDPARAMS, "Setting height=0 returned %#x.\n", hr);
5822 reset_ddsd(&ddsd);
5823 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5824 ok(SUCCEEDED(hr), "GetSurfaceDesc failed, hr %#x.\n", hr);
5825 ok(ddsd.dwWidth == 8, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
5826 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
5828 /* Pitch and width can be set, but only together, and only with LPSURFACE. They must not be 0 */
5829 reset_ddsd(&ddsd);
5830 ddsd.dwFlags = DDSD_PITCH;
5831 U1(ddsd).lPitch = 8 * 4;
5832 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5833 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch without lpSurface or width returned %#x.\n", hr);
5835 ddsd.dwFlags = DDSD_WIDTH;
5836 ddsd.dwWidth = 16;
5837 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5838 ok(hr == DDERR_INVALIDPARAMS, "Setting width without lpSurface or pitch returned %#x.\n", hr);
5840 ddsd.dwFlags = DDSD_PITCH | DDSD_LPSURFACE;
5841 ddsd.lpSurface = data;
5842 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5843 ok(hr == DDERR_INVALIDPARAMS, "Setting pitch and lpSurface without width returned %#x.\n", hr);
5845 ddsd.dwFlags = DDSD_WIDTH | DDSD_LPSURFACE;
5846 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5847 ok(hr == DDERR_INVALIDPARAMS, "Setting width and lpSurface without pitch returned %#x.\n", hr);
5849 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5850 U1(ddsd).lPitch = 16 * 4;
5851 ddsd.dwWidth = 16;
5852 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5853 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5855 reset_ddsd(&ddsd);
5856 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &ddsd);
5857 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
5858 ok(ddsd.dwWidth == 16, "SetSurfaceDesc: Expected width 8, got %u.\n", ddsd.dwWidth);
5859 ok(ddsd.dwHeight == 16, "SetSurfaceDesc: Expected height 16, got %u.\n", ddsd.dwHeight);
5860 ok(U1(ddsd).lPitch == 16 * 4, "SetSurfaceDesc: Expected pitch 64, got %u.\n", U1(ddsd).lPitch);
5862 /* The pitch must be 32 bit aligned and > 0, but is not verified for sanity otherwise.
5864 * VMware rejects those calls, but all real drivers accept it. Mark the VMware behavior broken. */
5865 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5866 U1(ddsd).lPitch = 4 * 4;
5867 ddsd.lpSurface = data;
5868 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5869 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
5871 U1(ddsd).lPitch = 4;
5872 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5873 ok(SUCCEEDED(hr) || broken(hr == DDERR_INVALIDPARAMS), "Failed to set surface desc, hr %#x.\n", hr);
5875 U1(ddsd).lPitch = 16 * 4 + 1;
5876 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5877 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
5879 U1(ddsd).lPitch = 16 * 4 + 3;
5880 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5881 ok(hr == DDERR_INVALIDPARAMS, "Setting misaligned pitch returned %#x.\n", hr);
5883 U1(ddsd).lPitch = -4;
5884 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5885 ok(hr == DDERR_INVALIDPARAMS, "Setting negative pitch returned %#x.\n", hr);
5887 U1(ddsd).lPitch = 16 * 4;
5888 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5889 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5891 reset_ddsd(&ddsd);
5892 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5893 U1(ddsd).lPitch = 0;
5894 ddsd.dwWidth = 16;
5895 ddsd.lpSurface = data;
5896 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5897 ok(hr == DDERR_INVALIDPARAMS, "Setting zero pitch returned %#x.\n", hr);
5899 ddsd.dwFlags = DDSD_WIDTH | DDSD_PITCH | DDSD_LPSURFACE;
5900 U1(ddsd).lPitch = 16 * 4;
5901 ddsd.dwWidth = 0;
5902 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5903 ok(hr == DDERR_INVALIDPARAMS, "Setting zero width returned %#x.\n", hr);
5905 /* Setting the pixelformat without LPSURFACE is an error, but with LPSURFACE it works. */
5906 ddsd.dwFlags = DDSD_PIXELFORMAT;
5907 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5908 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5909 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5910 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5911 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5912 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5913 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5914 ok(hr == DDERR_INVALIDPARAMS, "Setting the pixel format returned %#x.\n", hr);
5916 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_LPSURFACE;
5917 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5918 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5920 /* Can't set color keys. */
5921 reset_ddsd(&ddsd);
5922 ddsd.dwFlags = DDSD_CKSRCBLT;
5923 ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = 0x00ff0000;
5924 ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0x00ff0000;
5925 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5926 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
5928 ddsd.dwFlags = DDSD_CKSRCBLT | DDSD_LPSURFACE;
5929 ddsd.lpSurface = data;
5930 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5931 ok(hr == DDERR_INVALIDPARAMS, "Setting ddckCKSrcBlt returned %#x.\n", hr);
5933 IDirectDrawSurface4_Release(surface);
5935 /* SetSurfaceDesc needs systemmemory surfaces.
5937 * As a sidenote, fourcc surfaces aren't allowed in sysmem, thus testing DDSD_LINEARSIZE is moot. */
5938 for (i = 0; i < sizeof(invalid_caps_tests) / sizeof(*invalid_caps_tests); i++)
5940 reset_ddsd(&ddsd);
5941 ddsd.dwFlags = DDSD_CAPS;
5942 ddsd.ddsCaps.dwCaps = invalid_caps_tests[i].caps;
5943 ddsd.ddsCaps.dwCaps2 = invalid_caps_tests[i].caps2;
5944 if (!(invalid_caps_tests[i].caps & DDSCAPS_PRIMARYSURFACE))
5946 ddsd.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
5947 ddsd.dwWidth = 8;
5948 ddsd.dwHeight = 8;
5949 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
5950 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
5951 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
5952 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
5953 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
5954 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
5957 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
5958 ok(SUCCEEDED(hr) || hr == DDERR_NODIRECTDRAWHW, "Failed to create surface, hr %#x.\n", hr);
5959 if (FAILED(hr))
5961 skip("Cannot create a %s surface, skipping vidmem SetSurfaceDesc test.\n",
5962 invalid_caps_tests[i].name);
5963 goto done;
5966 reset_ddsd(&ddsd);
5967 ddsd.dwFlags = DDSD_LPSURFACE;
5968 ddsd.lpSurface = data;
5969 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5970 if (invalid_caps_tests[i].supported)
5972 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
5974 else
5976 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
5977 invalid_caps_tests[i].name, hr);
5979 /* Check priority of error conditions. */
5980 ddsd.dwFlags = DDSD_WIDTH;
5981 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
5982 ok(hr == DDERR_INVALIDSURFACETYPE, "SetSurfaceDesc on a %s surface returned %#x.\n",
5983 invalid_caps_tests[i].name, hr);
5986 IDirectDrawSurface4_Release(surface);
5989 done:
5990 ref = IDirectDraw4_Release(ddraw);
5991 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
5992 DestroyWindow(window);
5995 static void test_user_memory_getdc(void)
5997 IDirectDraw4 *ddraw;
5998 HWND window;
5999 HRESULT hr;
6000 DDSURFACEDESC2 ddsd;
6001 IDirectDrawSurface4 *surface;
6002 DWORD data[16][16];
6003 ULONG ref;
6004 HDC dc;
6005 unsigned int x, y;
6007 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6008 0, 0, 640, 480, 0, 0, 0, 0);
6009 ddraw = create_ddraw();
6010 ok(!!ddraw, "Failed to create a ddraw object.\n");
6012 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6013 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6015 reset_ddsd(&ddsd);
6016 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
6017 ddsd.dwWidth = 16;
6018 ddsd.dwHeight = 16;
6019 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6020 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6021 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6022 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6023 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6024 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6025 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6026 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6027 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6029 memset(data, 0xaa, sizeof(data));
6030 reset_ddsd(&ddsd);
6031 ddsd.dwFlags = DDSD_LPSURFACE;
6032 ddsd.lpSurface = data;
6033 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6034 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6036 hr = IDirectDrawSurface4_GetDC(surface, &dc);
6037 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6038 BitBlt(dc, 0, 0, 16, 8, NULL, 0, 0, WHITENESS);
6039 BitBlt(dc, 0, 8, 16, 8, NULL, 0, 0, BLACKNESS);
6040 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
6041 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6043 ok(data[0][0] == 0xffffffff, "Expected color 0xffffffff, got %#x.\n", data[0][0]);
6044 ok(data[15][15] == 0x00000000, "Expected color 0x00000000, got %#x.\n", data[15][15]);
6046 ddsd.dwFlags = DDSD_LPSURFACE | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH;
6047 ddsd.lpSurface = data;
6048 ddsd.dwWidth = 4;
6049 ddsd.dwHeight = 8;
6050 U1(ddsd).lPitch = sizeof(*data);
6051 hr = IDirectDrawSurface4_SetSurfaceDesc(surface, &ddsd, 0);
6052 ok(SUCCEEDED(hr), "Failed to set surface desc, hr %#x.\n", hr);
6054 memset(data, 0xaa, sizeof(data));
6055 hr = IDirectDrawSurface4_GetDC(surface, &dc);
6056 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
6057 BitBlt(dc, 0, 0, 4, 8, NULL, 0, 0, BLACKNESS);
6058 BitBlt(dc, 1, 1, 2, 2, NULL, 0, 0, WHITENESS);
6059 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
6060 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
6062 for (y = 0; y < 4; y++)
6064 for (x = 0; x < 4; x++)
6066 if ((x == 1 || x == 2) && (y == 1 || y == 2))
6067 ok(data[y][x] == 0xffffffff, "Expected color 0xffffffff on position %ux%u, got %#x.\n",
6068 x, y, data[y][x]);
6069 else
6070 ok(data[y][x] == 0x00000000, "Expected color 0x00000000 on position %ux%u, got %#x.\n",
6071 x, y, data[y][x]);
6074 ok(data[0][5] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 5x0, got %#x.\n",
6075 data[0][5]);
6076 ok(data[7][3] == 0x00000000, "Expected color 0x00000000 on position 3x7, got %#x.\n",
6077 data[7][3]);
6078 ok(data[7][4] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 4x7, got %#x.\n",
6079 data[7][4]);
6080 ok(data[8][0] == 0xaaaaaaaa, "Expected color 0xaaaaaaaa on position 0x8, got %#x.\n",
6081 data[8][0]);
6083 IDirectDrawSurface4_Release(surface);
6084 ref = IDirectDraw4_Release(ddraw);
6085 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6086 DestroyWindow(window);
6089 static void test_sysmem_overlay(void)
6091 IDirectDraw4 *ddraw;
6092 HWND window;
6093 HRESULT hr;
6094 DDSURFACEDESC2 ddsd;
6095 IDirectDrawSurface4 *surface;
6096 ULONG ref;
6098 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6099 0, 0, 640, 480, 0, 0, 0, 0);
6100 ddraw = create_ddraw();
6101 ok(!!ddraw, "Failed to create a ddraw object.\n");
6103 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6104 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6106 reset_ddsd(&ddsd);
6107 ddsd.dwFlags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
6108 ddsd.dwWidth = 16;
6109 ddsd.dwHeight = 16;
6110 ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OVERLAY;
6111 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
6112 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_RGB;
6113 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 32;
6114 U2(U4(ddsd).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6115 U3(U4(ddsd).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6116 U4(U4(ddsd).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6117 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &surface, NULL);
6118 ok(hr == DDERR_NOOVERLAYHW, "Got unexpected hr %#x.\n", hr);
6120 ref = IDirectDraw4_Release(ddraw);
6121 ok(ref == 0, "Ddraw object not properly released, refcount %u.\n", ref);
6122 DestroyWindow(window);
6125 static void test_primary_palette(void)
6127 DDSCAPS2 surface_caps = {DDSCAPS_FLIP, 0, 0, {0}};
6128 IDirectDrawSurface4 *primary, *backbuffer;
6129 PALETTEENTRY palette_entries[256];
6130 IDirectDrawPalette *palette, *tmp;
6131 DDSURFACEDESC2 surface_desc;
6132 IDirectDraw4 *ddraw;
6133 DWORD palette_caps;
6134 ULONG refcount;
6135 HWND window;
6136 HRESULT hr;
6138 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6139 0, 0, 640, 480, 0, 0, 0, 0);
6140 ddraw = create_ddraw();
6141 ok(!!ddraw, "Failed to create a ddraw object.\n");
6142 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
6144 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
6145 IDirectDraw4_Release(ddraw);
6146 DestroyWindow(window);
6147 return;
6149 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6150 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6152 memset(&surface_desc, 0, sizeof(surface_desc));
6153 surface_desc.dwSize = sizeof(surface_desc);
6154 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
6155 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
6156 U5(surface_desc).dwBackBufferCount = 1;
6157 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
6158 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6159 hr = IDirectDrawSurface4_GetAttachedSurface(primary, &surface_caps, &backbuffer);
6160 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6162 memset(palette_entries, 0, sizeof(palette_entries));
6163 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256, palette_entries, &palette, NULL);
6164 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
6165 refcount = get_refcount((IUnknown *)palette);
6166 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6168 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6169 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6170 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
6172 hr = IDirectDrawSurface4_SetPalette(primary, palette);
6173 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6175 /* The Windows 8 testbot attaches the palette to the backbuffer as well,
6176 * and is generally somewhat broken with respect to 8 bpp / palette
6177 * handling. */
6178 if (SUCCEEDED(IDirectDrawSurface4_GetPalette(backbuffer, &tmp)))
6180 win_skip("Broken palette handling detected, skipping tests.\n");
6181 IDirectDrawPalette_Release(tmp);
6182 IDirectDrawPalette_Release(palette);
6183 /* The Windows 8 testbot keeps extra references to the primary and
6184 * backbuffer while in 8 bpp mode. */
6185 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
6186 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
6187 goto done;
6190 refcount = get_refcount((IUnknown *)palette);
6191 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6193 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6194 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6195 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE | DDPCAPS_ALLOW256),
6196 "Got unexpected palette caps %#x.\n", palette_caps);
6198 hr = IDirectDrawSurface4_SetPalette(primary, NULL);
6199 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6200 refcount = get_refcount((IUnknown *)palette);
6201 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6203 hr = IDirectDrawPalette_GetCaps(palette, &palette_caps);
6204 ok(SUCCEEDED(hr), "Failed to get palette caps, hr %#x.\n", hr);
6205 ok(palette_caps == (DDPCAPS_8BIT | DDPCAPS_ALLOW256), "Got unexpected palette caps %#x.\n", palette_caps);
6207 hr = IDirectDrawSurface4_SetPalette(primary, palette);
6208 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
6209 refcount = get_refcount((IUnknown *)palette);
6210 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6212 hr = IDirectDrawSurface4_GetPalette(primary, &tmp);
6213 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
6214 ok(tmp == palette, "Got unexpected palette %p, expected %p.\n", tmp, palette);
6215 IDirectDrawPalette_Release(tmp);
6216 hr = IDirectDrawSurface4_GetPalette(backbuffer, &tmp);
6217 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6219 refcount = IDirectDrawPalette_Release(palette);
6220 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6221 refcount = IDirectDrawPalette_Release(palette);
6222 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6224 /* Note that this only seems to work when the palette is attached to the
6225 * primary surface. When attached to a regular surface, attempting to get
6226 * the palette here will cause an access violation. */
6227 hr = IDirectDrawSurface4_GetPalette(primary, &tmp);
6228 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
6230 done:
6231 refcount = IDirectDrawSurface4_Release(backbuffer);
6232 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6233 refcount = IDirectDrawSurface4_Release(primary);
6234 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6235 refcount = IDirectDraw4_Release(ddraw);
6236 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6237 DestroyWindow(window);
6240 static HRESULT WINAPI surface_counter(IDirectDrawSurface4 *surface, DDSURFACEDESC2 *desc, void *context)
6242 UINT *surface_count = context;
6244 ++(*surface_count);
6245 IDirectDrawSurface_Release(surface);
6247 return DDENUMRET_OK;
6250 static void test_surface_attachment(void)
6252 IDirectDrawSurface4 *surface1, *surface2, *surface3, *surface4;
6253 IDirectDrawSurface *surface1v1, *surface2v1;
6254 DDSCAPS2 caps = {DDSCAPS_TEXTURE, 0, 0, {0}};
6255 DDSURFACEDESC2 surface_desc;
6256 IDirectDraw4 *ddraw;
6257 UINT surface_count;
6258 ULONG refcount;
6259 HWND window;
6260 HRESULT hr;
6262 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6263 0, 0, 640, 480, 0, 0, 0, 0);
6264 ddraw = create_ddraw();
6265 ok(!!ddraw, "Failed to create a ddraw object.\n");
6266 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6267 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6269 memset(&surface_desc, 0, sizeof(surface_desc));
6270 surface_desc.dwSize = sizeof(surface_desc);
6271 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
6272 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6273 U2(surface_desc).dwMipMapCount = 3;
6274 surface_desc.dwWidth = 128;
6275 surface_desc.dwHeight = 128;
6276 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6277 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6279 hr = IDirectDrawSurface4_GetAttachedSurface(surface1, &caps, &surface2);
6280 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
6281 hr = IDirectDrawSurface4_GetAttachedSurface(surface2, &caps, &surface3);
6282 ok(SUCCEEDED(hr), "Failed to get mip level, hr %#x.\n", hr);
6283 hr = IDirectDrawSurface4_GetAttachedSurface(surface3, &caps, &surface4);
6284 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6286 surface_count = 0;
6287 IDirectDrawSurface4_EnumAttachedSurfaces(surface1, &surface_count, surface_counter);
6288 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
6289 surface_count = 0;
6290 IDirectDrawSurface4_EnumAttachedSurfaces(surface2, &surface_count, surface_counter);
6291 ok(surface_count == 1, "Got unexpected surface_count %u.\n", surface_count);
6292 surface_count = 0;
6293 IDirectDrawSurface4_EnumAttachedSurfaces(surface3, &surface_count, surface_counter);
6294 ok(!surface_count, "Got unexpected surface_count %u.\n", surface_count);
6296 memset(&surface_desc, 0, sizeof(surface_desc));
6297 surface_desc.dwSize = sizeof(surface_desc);
6298 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6299 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
6300 surface_desc.dwWidth = 16;
6301 surface_desc.dwHeight = 16;
6302 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
6303 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6305 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4);
6306 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6307 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
6308 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6309 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface4);
6310 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6311 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface3);
6312 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6313 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface4);
6314 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6315 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface2);
6316 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6318 IDirectDrawSurface4_Release(surface4);
6320 memset(&surface_desc, 0, sizeof(surface_desc));
6321 surface_desc.dwSize = sizeof(surface_desc);
6322 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6323 surface_desc.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
6324 surface_desc.dwWidth = 16;
6325 surface_desc.dwHeight = 16;
6326 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
6327 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6329 if (SUCCEEDED(hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4)))
6331 skip("Running on refrast, skipping some tests.\n");
6332 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface4);
6333 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6335 else
6337 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6338 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
6339 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6340 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface4);
6341 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6342 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface3);
6343 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6344 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface4);
6345 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6346 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface2);
6347 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6350 IDirectDrawSurface4_Release(surface4);
6351 IDirectDrawSurface4_Release(surface3);
6352 IDirectDrawSurface4_Release(surface2);
6353 IDirectDrawSurface4_Release(surface1);
6355 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
6356 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6358 /* Try a single primary and two offscreen plain surfaces. */
6359 memset(&surface_desc, 0, sizeof(surface_desc));
6360 surface_desc.dwSize = sizeof(surface_desc);
6361 surface_desc.dwFlags = DDSD_CAPS;
6362 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6363 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6364 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6366 memset(&surface_desc, 0, sizeof(surface_desc));
6367 surface_desc.dwSize = sizeof(surface_desc);
6368 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6369 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6370 surface_desc.dwWidth = registry_mode.dmPelsWidth;
6371 surface_desc.dwHeight = registry_mode.dmPelsHeight;
6372 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
6373 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6375 memset(&surface_desc, 0, sizeof(surface_desc));
6376 surface_desc.dwSize = sizeof(surface_desc);
6377 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6378 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6379 surface_desc.dwWidth = registry_mode.dmPelsWidth;
6380 surface_desc.dwHeight = registry_mode.dmPelsHeight;
6381 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
6382 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6384 /* This one has a different size. */
6385 memset(&surface_desc, 0, sizeof(surface_desc));
6386 surface_desc.dwSize = sizeof(surface_desc);
6387 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
6388 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
6389 surface_desc.dwWidth = 128;
6390 surface_desc.dwHeight = 128;
6391 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface4, NULL);
6392 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6394 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
6395 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6396 /* Try the reverse without detaching first. */
6397 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface1);
6398 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
6399 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
6400 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6402 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface1);
6403 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6404 /* Try to detach reversed. */
6405 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
6406 ok(hr == DDERR_CANNOTDETACHSURFACE, "Got unexpected hr %#x.\n", hr);
6407 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface2, 0, surface1);
6408 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6410 hr = IDirectDrawSurface4_AddAttachedSurface(surface2, surface3);
6411 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6412 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface2, 0, surface3);
6413 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6415 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface4);
6416 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6417 hr = IDirectDrawSurface4_AddAttachedSurface(surface4, surface1);
6418 ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6420 IDirectDrawSurface4_Release(surface4);
6421 IDirectDrawSurface4_Release(surface3);
6422 IDirectDrawSurface4_Release(surface2);
6423 IDirectDrawSurface4_Release(surface1);
6425 /* Test DeleteAttachedSurface() and automatic detachment of attached surfaces on release. */
6426 memset(&surface_desc, 0, sizeof(surface_desc));
6427 surface_desc.dwSize = sizeof(surface_desc);
6428 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
6429 surface_desc.dwWidth = 64;
6430 surface_desc.dwHeight = 64;
6431 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
6432 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6433 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB; /* D3DFMT_R5G6B5 */
6434 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 16;
6435 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0xf800;
6436 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x07e0;
6437 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x001f;
6438 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface1, NULL);
6439 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6440 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface3, NULL);
6441 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6443 surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
6444 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
6445 U1(U4(surface_desc).ddpfPixelFormat).dwZBufferBitDepth = 16;
6446 U3(U4(surface_desc).ddpfPixelFormat).dwZBitMask = 0x0000ffff;
6447 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface2, NULL);
6448 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6450 hr = IDirectDrawSurface4_QueryInterface(surface1, &IID_IDirectDrawSurface, (void **)&surface1v1);
6451 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
6452 hr = IDirectDrawSurface4_QueryInterface(surface2, &IID_IDirectDrawSurface, (void **)&surface2v1);
6453 ok(SUCCEEDED(hr), "Failed to get interface, hr %#x.\n", hr);
6455 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
6456 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6457 refcount = get_refcount((IUnknown *)surface2);
6458 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6459 refcount = get_refcount((IUnknown *)surface2v1);
6460 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6461 hr = IDirectDrawSurface4_AddAttachedSurface(surface1, surface2);
6462 ok(hr == DDERR_SURFACEALREADYATTACHED, "Got unexpected hr %#x.\n", hr);
6463 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
6464 todo_wine ok(hr == DDERR_CANNOTATTACHSURFACE, "Got unexpected hr %#x.\n", hr);
6465 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
6466 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
6468 /* Attaching while already attached to other surface. */
6469 hr = IDirectDrawSurface4_AddAttachedSurface(surface3, surface2);
6470 todo_wine ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6471 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface3, 0, surface2);
6472 todo_wine ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6473 IDirectDrawSurface4_Release(surface3);
6475 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
6476 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6477 refcount = get_refcount((IUnknown *)surface2);
6478 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6479 refcount = get_refcount((IUnknown *)surface2v1);
6480 ok(refcount == 1, "Got unexpected refcount %u.\n", refcount);
6482 /* DeleteAttachedSurface() when attaching via IDirectDrawSurface. */
6483 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
6484 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6485 hr = IDirectDrawSurface4_DeleteAttachedSurface(surface1, 0, surface2);
6486 ok(hr == DDERR_SURFACENOTATTACHED, "Got unexpected hr %#x.\n", hr);
6487 hr = IDirectDrawSurface_DeleteAttachedSurface(surface1v1, 0, surface2v1);
6488 ok(SUCCEEDED(hr), "Failed to detach surface, hr %#x.\n", hr);
6489 refcount = IDirectDrawSurface4_Release(surface2);
6490 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6491 refcount = IDirectDrawSurface4_Release(surface1);
6492 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6494 /* Automatic detachment on release. */
6495 hr = IDirectDrawSurface_AddAttachedSurface(surface1v1, surface2v1);
6496 ok(SUCCEEDED(hr), "Failed to attach surface, hr %#x.\n", hr);
6497 refcount = get_refcount((IUnknown *)surface2v1);
6498 ok(refcount == 2, "Got unexpected refcount %u.\n", refcount);
6499 refcount = IDirectDrawSurface_Release(surface1v1);
6500 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6501 refcount = IDirectDrawSurface_Release(surface2v1);
6502 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6503 refcount = IDirectDraw4_Release(ddraw);
6504 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6505 DestroyWindow(window);
6508 static void test_private_data(void)
6510 IDirectDraw4 *ddraw;
6511 IDirectDrawSurface4 *surface, *surface2;
6512 DDSURFACEDESC2 surface_desc;
6513 ULONG refcount, refcount2, refcount3;
6514 IUnknown *ptr;
6515 DWORD size = sizeof(ptr);
6516 HRESULT hr;
6517 HWND window;
6518 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
6519 DWORD data[] = {1, 2, 3, 4};
6520 DDCAPS hal_caps;
6521 static const GUID ddraw_private_data_test_guid =
6523 0xfdb37466,
6524 0x428f,
6525 0x4edf,
6526 {0xa3,0x7f,0x9b,0x1d,0xf4,0x88,0xc5,0xfc}
6528 static const GUID ddraw_private_data_test_guid2 =
6530 0x2e5afac2,
6531 0x87b5,
6532 0x4c10,
6533 {0x9b,0x4b,0x89,0xd7,0xd1,0x12,0xe7,0x2b}
6536 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6537 0, 0, 640, 480, 0, 0, 0, 0);
6538 ddraw = create_ddraw();
6539 ok(!!ddraw, "Failed to create a ddraw object.\n");
6540 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6541 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6543 reset_ddsd(&surface_desc);
6544 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
6545 surface_desc.ddsCaps.dwCaps |= DDSCAPS_OFFSCREENPLAIN;
6546 surface_desc.dwHeight = 4;
6547 surface_desc.dwWidth = 4;
6548 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6549 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6551 /* NULL pointers are not valid, but don't cause a crash. */
6552 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL,
6553 sizeof(IUnknown *), DDSPD_IUNKNOWNPOINTER);
6554 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6555 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 0, 0);
6556 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6557 hr = IDirectDrawSurface7_SetPrivateData(surface, &ddraw_private_data_test_guid, NULL, 1, 0);
6558 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6560 /* DDSPD_IUNKNOWNPOINTER needs sizeof(IUnknown *) bytes of data. */
6561 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6562 0, DDSPD_IUNKNOWNPOINTER);
6563 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6564 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6565 5, DDSPD_IUNKNOWNPOINTER);
6566 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6567 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6568 sizeof(ddraw) * 2, DDSPD_IUNKNOWNPOINTER);
6569 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6571 /* Note that with a size != 0 and size != sizeof(IUnknown *) and
6572 * DDSPD_IUNKNOWNPOINTER set SetPrivateData in ddraw4 and ddraw7
6573 * erases the old content and returns an error. This behavior has
6574 * been fixed in d3d8 and d3d9. Unless an application is found
6575 * that depends on this we don't care about this behavior. */
6576 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6577 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6578 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6579 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6580 0, DDSPD_IUNKNOWNPOINTER);
6581 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6582 size = sizeof(ptr);
6583 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
6584 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
6585 hr = IDirectDrawSurface4_FreePrivateData(surface, &ddraw_private_data_test_guid);
6586 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
6588 refcount = get_refcount((IUnknown *)ddraw);
6589 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6590 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6591 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6592 refcount2 = get_refcount((IUnknown *)ddraw);
6593 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
6595 hr = IDirectDrawSurface4_FreePrivateData(surface, &ddraw_private_data_test_guid);
6596 ok(SUCCEEDED(hr), "Failed to free private data, hr %#x.\n", hr);
6597 refcount2 = get_refcount((IUnknown *)ddraw);
6598 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
6600 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6601 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6602 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6603 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, surface,
6604 sizeof(surface), DDSPD_IUNKNOWNPOINTER);
6605 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6606 refcount2 = get_refcount((IUnknown *)ddraw);
6607 ok(refcount2 == refcount, "Got unexpected refcount %u.\n", refcount2);
6609 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, ddraw,
6610 sizeof(ddraw), DDSPD_IUNKNOWNPOINTER);
6611 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6612 size = 2 * sizeof(ptr);
6613 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
6614 ok(SUCCEEDED(hr), "Failed to get private data, hr %#x.\n", hr);
6615 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
6616 refcount2 = get_refcount(ptr);
6617 /* Object is NOT addref'ed by the getter. */
6618 ok(ptr == (IUnknown *)ddraw, "Returned interface pointer is %p, expected %p.\n", ptr, ddraw);
6619 ok(refcount2 == refcount + 1, "Got unexpected refcount %u.\n", refcount2);
6621 ptr = (IUnknown *)0xdeadbeef;
6622 size = 1;
6623 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
6624 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
6625 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
6626 size = 2 * sizeof(ptr);
6627 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, &size);
6628 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6629 ok(size == 2 * sizeof(ptr), "Got unexpected size %u.\n", size);
6630 size = 1;
6631 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, &ptr, &size);
6632 ok(hr == DDERR_MOREDATA, "Got unexpected hr %#x.\n", hr);
6633 ok(size == sizeof(ddraw), "Got unexpected size %u.\n", size);
6634 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
6635 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid2, NULL, NULL);
6636 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6637 size = 0xdeadbabe;
6638 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid2, &ptr, &size);
6639 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6640 ok(ptr == (IUnknown *)0xdeadbeef, "Got unexpected pointer %p.\n", ptr);
6641 ok(size == 0xdeadbabe, "Got unexpected size %u.\n", size);
6642 hr = IDirectDrawSurface4_GetPrivateData(surface, &ddraw_private_data_test_guid, NULL, NULL);
6643 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
6645 refcount3 = IDirectDrawSurface4_Release(surface);
6646 ok(!refcount3, "Got unexpected refcount %u.\n", refcount3);
6648 /* Destroying the surface frees the reference held on the private data. It also frees
6649 * the reference the surface is holding on its creating object. */
6650 refcount2 = get_refcount((IUnknown *)ddraw);
6651 ok(refcount2 == refcount - 1, "Got unexpected refcount %u.\n", refcount2);
6653 memset(&hal_caps, 0, sizeof(hal_caps));
6654 hal_caps.dwSize = sizeof(hal_caps);
6655 hr = IDirectDraw7_GetCaps(ddraw, &hal_caps, NULL);
6656 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6657 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) == (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6659 reset_ddsd(&surface_desc);
6660 surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_MIPMAPCOUNT;
6661 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
6662 surface_desc.dwHeight = 4;
6663 surface_desc.dwWidth = 4;
6664 U2(surface_desc).dwMipMapCount = 2;
6665 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6666 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6667 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &surface2);
6668 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6670 hr = IDirectDrawSurface4_SetPrivateData(surface, &ddraw_private_data_test_guid, data, sizeof(data), 0);
6671 ok(SUCCEEDED(hr), "Failed to set private data, hr %#x.\n", hr);
6672 hr = IDirectDrawSurface4_GetPrivateData(surface2, &ddraw_private_data_test_guid, NULL, NULL);
6673 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
6675 IDirectDrawSurface4_Release(surface2);
6676 IDirectDrawSurface4_Release(surface);
6678 else
6679 skip("Mipmapped textures not supported, skipping mipmap private data test.\n");
6681 refcount = IDirectDraw4_Release(ddraw);
6682 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6683 DestroyWindow(window);
6686 static void test_pixel_format(void)
6688 HWND window, window2 = NULL;
6689 HDC hdc, hdc2 = NULL;
6690 HMODULE gl = NULL;
6691 int format, test_format;
6692 PIXELFORMATDESCRIPTOR pfd;
6693 IDirectDraw4 *ddraw = NULL;
6694 IDirectDrawClipper *clipper = NULL;
6695 DDSURFACEDESC2 ddsd;
6696 IDirectDrawSurface4 *primary = NULL;
6697 DDBLTFX fx;
6698 HRESULT hr;
6700 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
6701 100, 100, 160, 160, NULL, NULL, NULL, NULL);
6702 if (!window)
6704 skip("Failed to create window\n");
6705 return;
6708 window2 = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
6709 100, 100, 160, 160, NULL, NULL, NULL, NULL);
6711 hdc = GetDC(window);
6712 if (!hdc)
6714 skip("Failed to get DC\n");
6715 goto cleanup;
6718 if (window2)
6719 hdc2 = GetDC(window2);
6721 gl = LoadLibraryA("opengl32.dll");
6722 ok(!!gl, "failed to load opengl32.dll; SetPixelFormat()/GetPixelFormat() may not work right\n");
6724 format = GetPixelFormat(hdc);
6725 ok(format == 0, "new window has pixel format %d\n", format);
6727 ZeroMemory(&pfd, sizeof(pfd));
6728 pfd.nSize = sizeof(pfd);
6729 pfd.nVersion = 1;
6730 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
6731 pfd.iPixelType = PFD_TYPE_RGBA;
6732 pfd.iLayerType = PFD_MAIN_PLANE;
6733 format = ChoosePixelFormat(hdc, &pfd);
6734 if (format <= 0)
6736 skip("no pixel format available\n");
6737 goto cleanup;
6740 if (!SetPixelFormat(hdc, format, &pfd) || GetPixelFormat(hdc) != format)
6742 skip("failed to set pixel format\n");
6743 goto cleanup;
6746 if (!hdc2 || !SetPixelFormat(hdc2, format, &pfd) || GetPixelFormat(hdc2) != format)
6748 skip("failed to set pixel format on second window\n");
6749 if (hdc2)
6751 ReleaseDC(window2, hdc2);
6752 hdc2 = NULL;
6756 ddraw = create_ddraw();
6757 ok(!!ddraw, "Failed to create a ddraw object.\n");
6759 test_format = GetPixelFormat(hdc);
6760 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6762 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6763 if (FAILED(hr))
6765 skip("Failed to set cooperative level, hr %#x.\n", hr);
6766 goto cleanup;
6769 test_format = GetPixelFormat(hdc);
6770 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6772 if (hdc2)
6774 hr = IDirectDraw4_CreateClipper(ddraw, 0, &clipper, NULL);
6775 ok(SUCCEEDED(hr), "Failed to create clipper, hr %#x.\n", hr);
6776 hr = IDirectDrawClipper_SetHWnd(clipper, 0, window2);
6777 ok(SUCCEEDED(hr), "Failed to set clipper window, hr %#x.\n", hr);
6779 test_format = GetPixelFormat(hdc);
6780 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6782 test_format = GetPixelFormat(hdc2);
6783 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6786 memset(&ddsd, 0, sizeof(ddsd));
6787 ddsd.dwSize = sizeof(ddsd);
6788 ddsd.dwFlags = DDSD_CAPS;
6789 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
6791 hr = IDirectDraw4_CreateSurface(ddraw, &ddsd, &primary, NULL);
6792 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n",hr);
6794 test_format = GetPixelFormat(hdc);
6795 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6797 if (hdc2)
6799 test_format = GetPixelFormat(hdc2);
6800 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6803 if (clipper)
6805 hr = IDirectDrawSurface4_SetClipper(primary, clipper);
6806 ok(SUCCEEDED(hr), "Failed to set clipper, hr %#x.\n", hr);
6808 test_format = GetPixelFormat(hdc);
6809 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6811 test_format = GetPixelFormat(hdc2);
6812 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6815 memset(&fx, 0, sizeof(fx));
6816 fx.dwSize = sizeof(fx);
6817 hr = IDirectDrawSurface4_Blt(primary, NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx);
6818 ok(SUCCEEDED(hr), "Failed to clear source surface, hr %#x.\n", hr);
6820 test_format = GetPixelFormat(hdc);
6821 ok(test_format == format, "window has pixel format %d, expected %d\n", test_format, format);
6823 if (hdc2)
6825 test_format = GetPixelFormat(hdc2);
6826 ok(test_format == format, "second window has pixel format %d, expected %d\n", test_format, format);
6829 cleanup:
6830 if (primary) IDirectDrawSurface4_Release(primary);
6831 if (clipper) IDirectDrawClipper_Release(clipper);
6832 if (ddraw) IDirectDraw4_Release(ddraw);
6833 if (gl) FreeLibrary(gl);
6834 if (hdc) ReleaseDC(window, hdc);
6835 if (hdc2) ReleaseDC(window2, hdc2);
6836 if (window) DestroyWindow(window);
6837 if (window2) DestroyWindow(window2);
6840 static void test_create_surface_pitch(void)
6842 IDirectDrawSurface4 *surface;
6843 DDSURFACEDESC2 surface_desc;
6844 IDirectDraw4 *ddraw;
6845 unsigned int i;
6846 ULONG refcount;
6847 HWND window;
6848 HRESULT hr;
6849 void *mem;
6851 static const struct
6853 DWORD placement;
6854 DWORD flags_in;
6855 DWORD pitch_in;
6856 HRESULT hr;
6857 DWORD flags_out;
6858 DWORD pitch_out32;
6859 DWORD pitch_out64;
6861 test_data[] =
6863 {DDSCAPS_VIDEOMEMORY, 0, 0, DD_OK,
6864 DDSD_PITCH, 0x100, 0x100},
6865 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x104, DD_OK,
6866 DDSD_PITCH, 0x100, 0x100},
6867 {DDSCAPS_VIDEOMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
6868 DDSD_PITCH, 0x100, 0x100},
6869 {DDSCAPS_VIDEOMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DDERR_INVALIDCAPS,
6870 0, 0, 0 },
6871 {DDSCAPS_SYSTEMMEMORY, 0, 0, DD_OK,
6872 DDSD_PITCH, 0x100, 0x0fc},
6873 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x104, DD_OK,
6874 DDSD_PITCH, 0x100, 0x0fc},
6875 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH, 0x0f8, DD_OK,
6876 DDSD_PITCH, 0x100, 0x0fc},
6877 {DDSCAPS_SYSTEMMEMORY, DDSD_PITCH | DDSD_LINEARSIZE, 0, DD_OK,
6878 DDSD_PITCH, 0x100, 0x0fc},
6879 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE, 0, DDERR_INVALIDPARAMS,
6880 0, 0, 0 },
6881 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x100, DD_OK,
6882 DDSD_PITCH, 0x100, 0x100},
6883 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0fe, DDERR_INVALIDPARAMS,
6884 0, 0, 0 },
6885 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0fc, DD_OK,
6886 DDSD_PITCH, 0x0fc, 0x0fc},
6887 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH, 0x0f8, DDERR_INVALIDPARAMS,
6888 0, 0, 0 },
6889 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x100, DDERR_INVALIDPARAMS,
6890 0, 0, 0 },
6891 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_LINEARSIZE, 0x3f00, DDERR_INVALIDPARAMS,
6892 0, 0, 0 },
6893 {DDSCAPS_SYSTEMMEMORY, DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE, 0x100, DD_OK,
6894 DDSD_PITCH, 0x100, 0x100},
6896 DWORD flags_mask = DDSD_PITCH | DDSD_LPSURFACE | DDSD_LINEARSIZE;
6898 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6899 0, 0, 640, 480, 0, 0, 0, 0);
6900 ddraw = create_ddraw();
6901 ok(!!ddraw, "Failed to create a ddraw object.\n");
6902 hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6903 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6905 mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ((63 * 4) + 8) * 63);
6907 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
6909 memset(&surface_desc, 0, sizeof(surface_desc));
6910 surface_desc.dwSize = sizeof(surface_desc);
6911 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | test_data[i].flags_in;
6912 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | test_data[i].placement;
6913 surface_desc.dwWidth = 63;
6914 surface_desc.dwHeight = 63;
6915 U1(surface_desc).lPitch = test_data[i].pitch_in;
6916 surface_desc.lpSurface = mem;
6917 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
6918 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
6919 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
6920 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
6921 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
6922 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
6923 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6924 ok(hr == test_data[i].hr || (test_data[i].placement == DDSCAPS_VIDEOMEMORY && hr == DDERR_NODIRECTDRAWHW),
6925 "Test %u: Got unexpected hr %#x, expected %#x.\n", i, hr, test_data[i].hr);
6926 if (FAILED(hr))
6927 continue;
6929 memset(&surface_desc, 0, sizeof(surface_desc));
6930 surface_desc.dwSize = sizeof(surface_desc);
6931 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
6932 ok(SUCCEEDED(hr), "Test %u: Failed to get surface desc, hr %#x.\n", i, hr);
6933 ok((surface_desc.dwFlags & flags_mask) == test_data[i].flags_out,
6934 "Test %u: Got unexpected flags %#x, expected %#x.\n",
6935 i, surface_desc.dwFlags & flags_mask, test_data[i].flags_out);
6936 if (sizeof(void *) != sizeof(DWORD) && test_data[i].pitch_out32 != test_data[i].pitch_out64)
6937 todo_wine ok(U1(surface_desc).lPitch == test_data[i].pitch_out64,
6938 "Test %u: Got unexpected pitch %u, expected %u.\n",
6939 i, U1(surface_desc).lPitch, test_data[i].pitch_out64);
6940 else
6941 ok(U1(surface_desc).lPitch == test_data[i].pitch_out32,
6942 "Test %u: Got unexpected pitch %u, expected %u.\n",
6943 i, U1(surface_desc).lPitch, test_data[i].pitch_out32);
6944 ok(!surface_desc.lpSurface, "Test %u: Got unexpected lpSurface %p.\n", i, surface_desc.lpSurface);
6946 IDirectDrawSurface4_Release(surface);
6949 HeapFree(GetProcessHeap(), 0, mem);
6950 refcount = IDirectDraw4_Release(ddraw);
6951 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
6952 DestroyWindow(window);
6955 static void test_mipmap_lock(void)
6957 IDirectDrawSurface4 *surface, *surface2;
6958 DDSURFACEDESC2 surface_desc;
6959 IDirectDraw4 *ddraw;
6960 ULONG refcount;
6961 HWND window;
6962 HRESULT hr;
6963 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
6964 DDCAPS hal_caps;
6966 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
6967 0, 0, 640, 480, 0, 0, 0, 0);
6968 ddraw = create_ddraw();
6969 ok(!!ddraw, "Failed to create a ddraw object.\n");
6970 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
6971 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
6973 memset(&hal_caps, 0, sizeof(hal_caps));
6974 hal_caps.dwSize = sizeof(hal_caps);
6975 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
6976 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
6977 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
6979 skip("Mipmapped textures not supported, skipping mipmap lock test.\n");
6980 IDirectDraw4_Release(ddraw);
6981 DestroyWindow(window);
6982 return;
6985 memset(&surface_desc, 0, sizeof(surface_desc));
6986 surface_desc.dwSize = sizeof(surface_desc);
6987 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_MIPMAPCOUNT;
6988 surface_desc.dwWidth = 4;
6989 surface_desc.dwHeight = 4;
6990 U2(surface_desc).dwMipMapCount = 2;
6991 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
6992 | DDSCAPS_SYSTEMMEMORY;
6993 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
6994 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
6995 hr = IDirectDrawSurface4_GetAttachedSurface(surface, &caps, &surface2);
6996 ok(SUCCEEDED(hr), "Failed to get attached surface, hr %#x.\n", hr);
6998 memset(&surface_desc, 0, sizeof(surface_desc));
6999 surface_desc.dwSize = sizeof(surface_desc);
7000 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
7001 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7002 memset(&surface_desc, 0, sizeof(surface_desc));
7003 surface_desc.dwSize = sizeof(surface_desc);
7004 hr = IDirectDrawSurface4_Lock(surface2, NULL, &surface_desc, 0, NULL);
7005 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
7006 IDirectDrawSurface4_Unlock(surface2, NULL);
7007 IDirectDrawSurface4_Unlock(surface, NULL);
7009 IDirectDrawSurface4_Release(surface2);
7010 IDirectDrawSurface4_Release(surface);
7011 refcount = IDirectDraw4_Release(ddraw);
7012 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7013 DestroyWindow(window);
7016 static void test_palette_complex(void)
7018 IDirectDrawSurface4 *surface, *mipmap, *tmp;
7019 DDSURFACEDESC2 surface_desc;
7020 IDirectDraw4 *ddraw;
7021 IDirectDrawPalette *palette, *palette2, *palette_mipmap;
7022 ULONG refcount;
7023 HWND window;
7024 HRESULT hr;
7025 DDSCAPS2 caps = {DDSCAPS_COMPLEX, 0, 0, {0}};
7026 DDCAPS hal_caps;
7027 PALETTEENTRY palette_entries[256];
7028 unsigned int i;
7029 HDC dc;
7030 RGBQUAD rgbquad;
7031 UINT count;
7033 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7034 0, 0, 640, 480, 0, 0, 0, 0);
7035 ddraw = create_ddraw();
7036 ok(!!ddraw, "Failed to create a ddraw object.\n");
7037 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7038 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7040 memset(&hal_caps, 0, sizeof(hal_caps));
7041 hal_caps.dwSize = sizeof(hal_caps);
7042 hr = IDirectDraw4_GetCaps(ddraw, &hal_caps, NULL);
7043 ok(SUCCEEDED(hr), "Failed to get caps, hr %#x.\n", hr);
7044 if ((hal_caps.ddsCaps.dwCaps & (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP)) != (DDSCAPS_TEXTURE | DDSCAPS_MIPMAP))
7046 skip("Mipmapped textures not supported, skipping mipmap palette test.\n");
7047 IDirectDraw4_Release(ddraw);
7048 DestroyWindow(window);
7049 return;
7052 memset(&surface_desc, 0, sizeof(surface_desc));
7053 surface_desc.dwSize = sizeof(surface_desc);
7054 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7055 surface_desc.dwWidth = 128;
7056 surface_desc.dwHeight = 128;
7057 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP;
7058 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7059 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7060 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7061 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7062 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7064 memset(palette_entries, 0, sizeof(palette_entries));
7065 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7066 palette_entries, &palette, NULL);
7067 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7069 memset(palette_entries, 0, sizeof(palette_entries));
7070 palette_entries[1].peRed = 0xff;
7071 palette_entries[1].peGreen = 0x80;
7072 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7073 palette_entries, &palette_mipmap, NULL);
7074 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7076 palette2 = (void *)0xdeadbeef;
7077 hr = IDirectDrawSurface4_GetPalette(surface, &palette2);
7078 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x.\n", hr);
7079 ok(!palette2, "Got unexpected palette %p.\n", palette2);
7080 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7081 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7082 hr = IDirectDrawSurface4_GetPalette(surface, &palette2);
7083 ok(SUCCEEDED(hr), "Failed to get palette, hr %#x.\n", hr);
7084 ok(palette == palette2, "Got unexpected palette %p.\n", palette2);
7085 IDirectDrawPalette_Release(palette2);
7087 mipmap = surface;
7088 IDirectDrawSurface4_AddRef(mipmap);
7089 for (i = 0; i < 7; ++i)
7091 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
7092 ok(SUCCEEDED(hr), "Failed to get attached surface, i %u, hr %#x.\n", i, hr);
7093 palette2 = (void *)0xdeadbeef;
7094 hr = IDirectDrawSurface4_GetPalette(tmp, &palette2);
7095 ok(hr == DDERR_NOPALETTEATTACHED, "Got unexpected hr %#x, i %u.\n", hr, i);
7096 ok(!palette2, "Got unexpected palette %p, i %u.\n", palette2, i);
7098 hr = IDirectDrawSurface4_SetPalette(tmp, palette_mipmap);
7099 ok(SUCCEEDED(hr), "Failed to set palette, i %u, hr %#x.\n", i, hr);
7101 hr = IDirectDrawSurface4_GetPalette(tmp, &palette2);
7102 ok(SUCCEEDED(hr), "Failed to get palette, i %u, hr %#x.\n", i, hr);
7103 ok(palette_mipmap == palette2, "Got unexpected palette %p.\n", palette2);
7104 IDirectDrawPalette_Release(palette2);
7106 hr = IDirectDrawSurface4_GetDC(tmp, &dc);
7107 ok(SUCCEEDED(hr), "Failed to get DC, i %u, hr %#x.\n", i, hr);
7108 count = GetDIBColorTable(dc, 1, 1, &rgbquad);
7109 ok(count == 1, "Expected count 1, got %u.\n", count);
7110 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x.\n", rgbquad.rgbRed);
7111 ok(rgbquad.rgbGreen == 0x80, "Expected rgbGreen = 0x80, got %#x.\n", rgbquad.rgbGreen);
7112 ok(rgbquad.rgbBlue == 0x0, "Expected rgbBlue = 0x0, got %#x.\n", rgbquad.rgbBlue);
7113 hr = IDirectDrawSurface4_ReleaseDC(tmp, dc);
7114 ok(SUCCEEDED(hr), "Failed to release DC, i %u, hr %#x.\n", i, hr);
7116 IDirectDrawSurface4_Release(mipmap);
7117 mipmap = tmp;
7120 hr = IDirectDrawSurface4_GetAttachedSurface(mipmap, &caps, &tmp);
7121 ok(hr == DDERR_NOTFOUND, "Got unexpected hr %#x.\n", hr);
7122 IDirectDrawSurface4_Release(mipmap);
7123 refcount = IDirectDrawSurface4_Release(surface);
7124 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7125 refcount = IDirectDrawPalette_Release(palette_mipmap);
7126 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7127 refcount = IDirectDrawPalette_Release(palette);
7128 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7130 refcount = IDirectDraw4_Release(ddraw);
7131 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7132 DestroyWindow(window);
7135 static void test_p8_rgb_blit(void)
7137 IDirectDrawSurface4 *src, *dst;
7138 DDSURFACEDESC2 surface_desc;
7139 IDirectDraw4 *ddraw;
7140 IDirectDrawPalette *palette;
7141 ULONG refcount;
7142 HWND window;
7143 HRESULT hr;
7144 PALETTEENTRY palette_entries[256];
7145 unsigned int x;
7146 static const BYTE src_data[] = {0x10, 0x1, 0x2, 0x3, 0x4, 0x5, 0xff, 0x80};
7147 static const D3DCOLOR expected[] =
7149 0x00101010, 0x00010101, 0x00020202, 0x00030303,
7150 0x00040404, 0x00050505, 0x00ffffff, 0x00808080,
7152 D3DCOLOR color;
7154 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7155 0, 0, 640, 480, 0, 0, 0, 0);
7156 ddraw = create_ddraw();
7157 ok(!!ddraw, "Failed to create a ddraw object.\n");
7158 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7159 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7161 memset(palette_entries, 0, sizeof(palette_entries));
7162 palette_entries[1].peGreen = 0xff;
7163 palette_entries[2].peBlue = 0xff;
7164 palette_entries[3].peFlags = 0xff;
7165 palette_entries[4].peRed = 0xff;
7166 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7167 palette_entries, &palette, NULL);
7168 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7170 memset(&surface_desc, 0, sizeof(surface_desc));
7171 surface_desc.dwSize = sizeof(surface_desc);
7172 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7173 surface_desc.dwWidth = 8;
7174 surface_desc.dwHeight = 1;
7175 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7176 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7177 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7178 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7179 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &src, NULL);
7180 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7182 memset(&surface_desc, 0, sizeof(surface_desc));
7183 surface_desc.dwSize = sizeof(surface_desc);
7184 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7185 surface_desc.dwWidth = 8;
7186 surface_desc.dwHeight = 1;
7187 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7188 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7189 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB | DDPF_ALPHAPIXELS;
7190 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7191 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7192 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7193 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7194 U5(U4(surface_desc).ddpfPixelFormat).dwRGBAlphaBitMask = 0xff000000;
7195 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &dst, NULL);
7196 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7198 memset(&surface_desc, 0, sizeof(surface_desc));
7199 surface_desc.dwSize = sizeof(surface_desc);
7200 hr = IDirectDrawSurface4_Lock(src, NULL, &surface_desc, 0, NULL);
7201 ok(SUCCEEDED(hr), "Failed to lock source surface, hr %#x.\n", hr);
7202 memcpy(surface_desc.lpSurface, src_data, sizeof(src_data));
7203 hr = IDirectDrawSurface4_Unlock(src, NULL);
7204 ok(SUCCEEDED(hr), "Failed to unlock source surface, hr %#x.\n", hr);
7206 hr = IDirectDrawSurface4_SetPalette(src, palette);
7207 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7208 hr = IDirectDrawSurface4_Blt(dst, NULL, src, NULL, DDBLT_WAIT, NULL);
7209 /* The r500 Windows 7 driver returns E_NOTIMPL. r200 on Windows XP works.
7210 * The Geforce 7 driver on Windows Vista returns E_FAIL. Newer Nvidia GPUs work. */
7211 ok(SUCCEEDED(hr) || broken(hr == E_NOTIMPL) || broken(hr == E_FAIL),
7212 "Failed to blit, hr %#x.\n", hr);
7214 if (SUCCEEDED(hr))
7216 for (x = 0; x < sizeof(expected) / sizeof(*expected); x++)
7218 color = get_surface_color(dst, x, 0);
7219 todo_wine ok(compare_color(color, expected[x], 0),
7220 "Pixel %u: Got color %#x, expected %#x.\n",
7221 x, color, expected[x]);
7225 IDirectDrawSurface4_Release(src);
7226 IDirectDrawSurface4_Release(dst);
7227 IDirectDrawPalette_Release(palette);
7229 refcount = IDirectDraw4_Release(ddraw);
7230 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7231 DestroyWindow(window);
7234 static void test_material(void)
7236 D3DMATERIALHANDLE mat_handle, tmp;
7237 IDirect3DMaterial3 *material;
7238 IDirect3DViewport3 *viewport;
7239 IDirect3DDevice3 *device;
7240 IDirectDrawSurface4 *rt;
7241 D3DCOLOR color;
7242 ULONG refcount;
7243 unsigned int i;
7244 HWND window;
7245 HRESULT hr;
7246 BOOL valid;
7248 static struct
7250 struct vec3 position;
7251 struct vec3 normal;
7252 D3DCOLOR diffuse;
7254 quad1[] =
7256 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7257 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7258 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7259 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffffffff},
7261 quad2[] =
7263 {{-1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7264 {{-1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7265 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7266 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, 0xffff0000},
7268 static const struct
7270 void *data;
7271 BOOL material;
7272 D3DCOLOR expected_color;
7274 test_data[] =
7276 {quad1, TRUE, 0x0000ff00},
7277 {quad2, TRUE, 0x0000ff00},
7278 {quad1, FALSE, 0x00ffffff},
7279 {quad2, FALSE, 0x00ff0000},
7281 static D3DRECT clear_rect = {{0}, {0}, {640}, {480}};
7283 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7284 0, 0, 640, 480, 0, 0, 0, 0);
7285 if (!(device = create_device(window, DDSCL_NORMAL)))
7287 skip("Failed to create a 3D device, skipping test.\n");
7288 DestroyWindow(window);
7289 return;
7292 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
7293 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
7295 viewport = create_viewport(device, 0, 0, 640, 480);
7296 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
7297 ok(SUCCEEDED(hr), "Failed to set current viewport, hr %#x.\n", hr);
7299 material = create_emissive_material(device, 0.0f, 1.0f, 0.0f, 0.0f);
7300 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
7301 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
7303 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
7304 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
7305 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
7306 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, mat_handle);
7307 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7308 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
7309 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
7310 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
7311 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, 0);
7312 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7313 hr = IDirect3DDevice3_GetLightState(device, D3DLIGHTSTATE_MATERIAL, &tmp);
7314 ok(SUCCEEDED(hr), "Failed to get light state, hr %#x.\n", hr);
7315 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
7317 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); ++i)
7319 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect,
7320 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
7321 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7323 hr = IDirect3DDevice3_SetLightState(device, D3DLIGHTSTATE_MATERIAL, test_data[i].material ? mat_handle : 0);
7324 ok(SUCCEEDED(hr), "Failed to set material state, hr %#x.\n", hr);
7326 hr = IDirect3DDevice3_BeginScene(device);
7327 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7328 hr = IDirect3DDevice2_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
7329 D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE, test_data[i].data, 4, 0);
7330 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7331 hr = IDirect3DDevice3_EndScene(device);
7332 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7333 color = get_surface_color(rt, 320, 240);
7334 ok(compare_color(color, test_data[i].expected_color, 1),
7335 "Got unexpected color 0x%08x, test %u.\n", color, i);
7338 destroy_material(material);
7339 material = create_diffuse_material(device, 1.0f, 0.0f, 0.0f, 1.0f);
7340 hr = IDirect3DMaterial3_GetHandle(material, device, &mat_handle);
7341 ok(SUCCEEDED(hr), "Failed to get material handle, hr %#x.\n", hr);
7343 hr = IDirect3DViewport3_SetBackground(viewport, mat_handle);
7344 ok(SUCCEEDED(hr), "Failed to set viewport background, hr %#x.\n", hr);
7345 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
7346 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
7347 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
7348 ok(valid, "Got unexpected valid %#x.\n", valid);
7349 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7350 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7351 color = get_surface_color(rt, 320, 240);
7352 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
7354 hr = IDirect3DViewport3_SetBackground(viewport, 0);
7355 ok(hr == DDERR_INVALIDPARAMS, "Got unexpected hr %#x.\n", hr);
7356 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
7357 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
7358 ok(tmp == mat_handle, "Got unexpected material handle %#x, expected %#x.\n", tmp, mat_handle);
7359 ok(valid, "Got unexpected valid %#x.\n", valid);
7360 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7361 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7362 color = get_surface_color(rt, 320, 240);
7363 ok(compare_color(color, 0x00ff0000, 1), "Got unexpected color 0x%08x.\n", color);
7365 destroy_viewport(device, viewport);
7366 viewport = create_viewport(device, 0, 0, 640, 480);
7368 hr = IDirect3DViewport3_GetBackground(viewport, &tmp, &valid);
7369 ok(SUCCEEDED(hr), "Failed to get viewport background, hr %#x.\n", hr);
7370 ok(!tmp, "Got unexpected material handle %#x.\n", tmp);
7371 ok(!valid, "Got unexpected valid %#x.\n", valid);
7372 hr = IDirect3DViewport3_Clear(viewport, 1, &clear_rect, D3DCLEAR_TARGET);
7373 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
7374 color = get_surface_color(rt, 320, 240);
7375 ok(compare_color(color, 0x00000000, 1), "Got unexpected color 0x%08x.\n", color);
7377 destroy_viewport(device, viewport);
7378 destroy_material(material);
7379 IDirectDrawSurface4_Release(rt);
7380 refcount = IDirect3DDevice3_Release(device);
7381 ok(!refcount, "Device has %u references left.\n", refcount);
7382 DestroyWindow(window);
7385 static void test_palette_gdi(void)
7387 IDirectDrawSurface4 *surface, *primary;
7388 DDSURFACEDESC2 surface_desc;
7389 IDirectDraw4 *ddraw;
7390 IDirectDrawPalette *palette, *palette2;
7391 ULONG refcount;
7392 HWND window;
7393 HRESULT hr;
7394 PALETTEENTRY palette_entries[256];
7395 UINT i;
7396 HDC dc;
7397 /* On the Windows 8 testbot palette index 0 of the onscreen palette is forced to
7398 * r = 0, g = 0, b = 0. Do not attempt to set it to something else as this is
7399 * not the point of this test. */
7400 static const RGBQUAD expected1[] =
7402 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
7403 {0x03, 0x00, 0x00, 0x00}, {0x15, 0x14, 0x13, 0x00},
7405 static const RGBQUAD expected2[] =
7407 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x01, 0x00}, {0x00, 0x02, 0x00, 0x00},
7408 {0x03, 0x00, 0x00, 0x00}, {0x25, 0x24, 0x23, 0x00},
7410 static const RGBQUAD expected3[] =
7412 {0x00, 0x00, 0x00, 0x00}, {0x40, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x40, 0x00},
7413 {0x00, 0x40, 0x00, 0x00}, {0x56, 0x34, 0x12, 0x00},
7415 HPALETTE ddraw_palette_handle;
7416 /* Similar to index 0, index 255 is r = 0xff, g = 0xff, b = 0xff on the Win8 VMs. */
7417 RGBQUAD rgbquad[255];
7418 static const RGBQUAD rgb_zero = {0, 0, 0, 0};
7420 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7421 0, 0, 640, 480, 0, 0, 0, 0);
7422 ddraw = create_ddraw();
7423 ok(!!ddraw, "Failed to create a ddraw object.\n");
7424 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7425 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7427 memset(&surface_desc, 0, sizeof(surface_desc));
7428 surface_desc.dwSize = sizeof(surface_desc);
7429 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7430 surface_desc.dwWidth = 16;
7431 surface_desc.dwHeight = 16;
7432 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7433 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7434 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
7435 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 8;
7436 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7437 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7439 /* Avoid colors from the Windows default palette. */
7440 memset(palette_entries, 0, sizeof(palette_entries));
7441 palette_entries[1].peRed = 0x01;
7442 palette_entries[2].peGreen = 0x02;
7443 palette_entries[3].peBlue = 0x03;
7444 palette_entries[4].peRed = 0x13;
7445 palette_entries[4].peGreen = 0x14;
7446 palette_entries[4].peBlue = 0x15;
7447 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7448 palette_entries, &palette, NULL);
7449 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7451 /* If there is no palette assigned and the display mode is not 8 bpp, some
7452 * drivers refuse to create a DC while others allow it. If a DC is created,
7453 * the DIB color table is uninitialized and contains random colors. No error
7454 * is generated when trying to read pixels and random garbage is returned.
7456 * The most likely explanation is that if the driver creates a DC, it (or
7457 * the higher-level runtime) uses GetSystemPaletteEntries to find the
7458 * palette, but GetSystemPaletteEntries fails when bpp > 8 and the palette
7459 * contains uninitialized garbage. See comments below for the P8 case. */
7461 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7462 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7463 hr = IDirectDrawSurface4_GetDC(surface, &dc);
7464 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7465 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
7466 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE),
7467 "Got unexpected palette %p, expected %p.\n",
7468 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
7470 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7471 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7472 for (i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
7474 ok(!memcmp(&rgbquad[i], &expected1[i], sizeof(rgbquad[i])),
7475 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7476 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7477 expected1[i].rgbRed, expected1[i].rgbGreen, expected1[i].rgbBlue);
7479 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7481 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7482 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7483 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7486 /* Update the palette while the DC is in use. This does not modify the DC. */
7487 palette_entries[4].peRed = 0x23;
7488 palette_entries[4].peGreen = 0x24;
7489 palette_entries[4].peBlue = 0x25;
7490 hr = IDirectDrawPalette_SetEntries(palette, 0, 4, 1, &palette_entries[4]);
7491 ok(SUCCEEDED(hr), "Failed to set palette entries, hr %#x.\n", hr);
7493 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
7494 ok(i == 1, "Expected count 1, got %u.\n", i);
7495 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
7496 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7497 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
7498 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
7500 /* Neither does re-setting the palette. */
7501 hr = IDirectDrawSurface4_SetPalette(surface, NULL);
7502 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7503 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7504 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7506 i = GetDIBColorTable(dc, 4, 1, &rgbquad[4]);
7507 ok(i == 1, "Expected count 1, got %u.\n", i);
7508 ok(!memcmp(&rgbquad[4], &expected1[4], sizeof(rgbquad[4])),
7509 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7510 i, rgbquad[4].rgbRed, rgbquad[4].rgbGreen, rgbquad[4].rgbBlue,
7511 expected1[4].rgbRed, expected1[4].rgbGreen, expected1[4].rgbBlue);
7513 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
7514 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7516 /* Refresh the DC. This updates the palette. */
7517 hr = IDirectDrawSurface4_GetDC(surface, &dc);
7518 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7519 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7520 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7521 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
7523 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7524 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7525 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7526 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7528 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7530 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7531 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7532 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7534 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
7535 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7537 refcount = IDirectDrawSurface4_Release(surface);
7538 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7540 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
7542 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
7543 IDirectDrawPalette_Release(palette);
7544 IDirectDraw4_Release(ddraw);
7545 DestroyWindow(window);
7546 return;
7548 ok(SUCCEEDED(hr), "Failed to set display mode, hr %#x.\n", hr);
7549 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
7550 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7552 memset(&surface_desc, 0, sizeof(surface_desc));
7553 surface_desc.dwSize = sizeof(surface_desc);
7554 surface_desc.dwFlags = DDSD_CAPS;
7555 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7556 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &primary, NULL);
7557 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7559 hr = IDirectDrawSurface4_SetPalette(primary, palette);
7560 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7561 hr = IDirectDrawSurface4_GetDC(primary, &dc);
7562 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7563 ddraw_palette_handle = SelectPalette(dc, GetStockObject(DEFAULT_PALETTE), FALSE);
7564 /* Windows 2000 on the testbot assigns a different palette to the primary. Refrast? */
7565 ok(ddraw_palette_handle == GetStockObject(DEFAULT_PALETTE) || broken(TRUE),
7566 "Got unexpected palette %p, expected %p.\n",
7567 ddraw_palette_handle, GetStockObject(DEFAULT_PALETTE));
7568 SelectPalette(dc, ddraw_palette_handle, FALSE);
7570 /* The primary uses the system palette. In exclusive mode, the system palette matches
7571 * the ddraw palette attached to the primary, so the result is what you would expect
7572 * from a regular surface. Tests for the interaction between the ddraw palette and
7573 * the system palette are not included pending an application that depends on this.
7574 * The relation between those causes problems on Windows Vista and newer for games
7575 * like Age of Empires or StarCraft. Don't emulate it without a real need. */
7576 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7577 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7578 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
7580 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7581 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7582 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7583 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7585 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7587 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7588 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7589 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7591 hr = IDirectDrawSurface4_ReleaseDC(primary, dc);
7592 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7594 memset(&surface_desc, 0, sizeof(surface_desc));
7595 surface_desc.dwSize = sizeof(surface_desc);
7596 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
7597 surface_desc.dwWidth = 16;
7598 surface_desc.dwHeight = 16;
7599 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7600 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7601 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7603 /* Here the offscreen surface appears to use the primary's palette,
7604 * but in all likelihood it is actually the system palette. */
7605 hr = IDirectDrawSurface4_GetDC(surface, &dc);
7606 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7607 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7608 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7609 for (i = 0; i < sizeof(expected2) / sizeof(*expected2); i++)
7611 ok(!memcmp(&rgbquad[i], &expected2[i], sizeof(rgbquad[i])),
7612 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7613 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7614 expected2[i].rgbRed, expected2[i].rgbGreen, expected2[i].rgbBlue);
7616 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7618 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7619 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7620 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7622 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
7623 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7625 /* On real hardware a change to the primary surface's palette applies immediately,
7626 * even on device contexts from offscreen surfaces that do not have their own
7627 * palette. On the testbot VMs this is not the case. Don't test this until we
7628 * know of an application that depends on this. */
7630 memset(palette_entries, 0, sizeof(palette_entries));
7631 palette_entries[1].peBlue = 0x40;
7632 palette_entries[2].peRed = 0x40;
7633 palette_entries[3].peGreen = 0x40;
7634 palette_entries[4].peRed = 0x12;
7635 palette_entries[4].peGreen = 0x34;
7636 palette_entries[4].peBlue = 0x56;
7637 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_8BIT | DDPCAPS_ALLOW256,
7638 palette_entries, &palette2, NULL);
7639 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7640 hr = IDirectDrawSurface4_SetPalette(surface, palette2);
7641 ok(SUCCEEDED(hr), "Failed to set palette, hr %#x.\n", hr);
7643 /* A palette assigned to the offscreen surface overrides the primary / system
7644 * palette. */
7645 hr = IDirectDrawSurface4_GetDC(surface, &dc);
7646 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x.\n", hr);
7647 i = GetDIBColorTable(dc, 0, sizeof(rgbquad) / sizeof(*rgbquad), rgbquad);
7648 ok(i == sizeof(rgbquad) / sizeof(*rgbquad), "Expected count 255, got %u.\n", i);
7649 for (i = 0; i < sizeof(expected3) / sizeof(*expected3); i++)
7651 ok(!memcmp(&rgbquad[i], &expected3[i], sizeof(rgbquad[i])),
7652 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=%#x g=%#x b=%#x.\n",
7653 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue,
7654 expected3[i].rgbRed, expected3[i].rgbGreen, expected3[i].rgbBlue);
7656 for (; i < sizeof(rgbquad) / sizeof(*rgbquad); i++)
7658 ok(!memcmp(&rgbquad[i], &rgb_zero, sizeof(rgbquad[i])),
7659 "Got color table entry %u r=%#x g=%#x b=%#x, expected r=0 g=0 b=0.\n",
7660 i, rgbquad[i].rgbRed, rgbquad[i].rgbGreen, rgbquad[i].rgbBlue);
7662 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
7663 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7665 refcount = IDirectDrawSurface4_Release(surface);
7666 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7668 /* The Windows 8 testbot keeps extra references to the primary and
7669 * backbuffer while in 8 bpp mode. */
7670 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
7671 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
7673 refcount = IDirectDrawSurface4_Release(primary);
7674 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7675 refcount = IDirectDrawPalette_Release(palette2);
7676 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7677 refcount = IDirectDrawPalette_Release(palette);
7678 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7679 refcount = IDirectDraw4_Release(ddraw);
7680 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7681 DestroyWindow(window);
7684 static void test_palette_alpha(void)
7686 IDirectDrawSurface4 *surface;
7687 DDSURFACEDESC2 surface_desc;
7688 IDirectDraw4 *ddraw;
7689 IDirectDrawPalette *palette;
7690 ULONG refcount;
7691 HWND window;
7692 HRESULT hr;
7693 PALETTEENTRY palette_entries[256];
7694 unsigned int i;
7695 static const struct
7697 DWORD caps, flags;
7698 BOOL attach_allowed;
7699 const char *name;
7701 test_data[] =
7703 {DDSCAPS_OFFSCREENPLAIN, DDSD_WIDTH | DDSD_HEIGHT, FALSE, "offscreenplain"},
7704 {DDSCAPS_TEXTURE, DDSD_WIDTH | DDSD_HEIGHT, TRUE, "texture"},
7705 {DDSCAPS_PRIMARYSURFACE, 0, FALSE, "primary"}
7708 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7709 0, 0, 640, 480, 0, 0, 0, 0);
7710 ddraw = create_ddraw();
7711 ok(!!ddraw, "Failed to create a ddraw object.\n");
7712 if (FAILED(IDirectDraw4_SetDisplayMode(ddraw, 640, 480, 8, 0, 0)))
7714 win_skip("Failed to set 8 bpp display mode, skipping test.\n");
7715 IDirectDraw4_Release(ddraw);
7716 DestroyWindow(window);
7717 return;
7719 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7720 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7722 memset(palette_entries, 0, sizeof(palette_entries));
7723 palette_entries[1].peFlags = 0x42;
7724 palette_entries[2].peFlags = 0xff;
7725 palette_entries[3].peFlags = 0x80;
7726 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, palette_entries, &palette, NULL);
7727 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7729 memset(palette_entries, 0x66, sizeof(palette_entries));
7730 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
7731 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
7732 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7733 palette_entries[0].peFlags);
7734 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7735 palette_entries[1].peFlags);
7736 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
7737 palette_entries[2].peFlags);
7738 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
7739 palette_entries[3].peFlags);
7741 IDirectDrawPalette_Release(palette);
7743 memset(palette_entries, 0, sizeof(palette_entries));
7744 palette_entries[1].peFlags = 0x42;
7745 palette_entries[1].peRed = 0xff;
7746 palette_entries[2].peFlags = 0xff;
7747 palette_entries[3].peFlags = 0x80;
7748 hr = IDirectDraw4_CreatePalette(ddraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_ALPHA,
7749 palette_entries, &palette, NULL);
7750 ok(SUCCEEDED(hr), "Failed to create palette, hr %#x.\n", hr);
7752 memset(palette_entries, 0x66, sizeof(palette_entries));
7753 hr = IDirectDrawPalette_GetEntries(palette, 0, 1, 4, palette_entries);
7754 ok(SUCCEEDED(hr), "Failed to get palette entries, hr %#x.\n", hr);
7755 ok(palette_entries[0].peFlags == 0x42, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7756 palette_entries[0].peFlags);
7757 ok(palette_entries[1].peFlags == 0xff, "Got unexpected peFlags 0x%02x, expected 0xff.\n",
7758 palette_entries[1].peFlags);
7759 ok(palette_entries[2].peFlags == 0x80, "Got unexpected peFlags 0x%02x, expected 0x80.\n",
7760 palette_entries[2].peFlags);
7761 ok(palette_entries[3].peFlags == 0x00, "Got unexpected peFlags 0x%02x, expected 0x00.\n",
7762 palette_entries[3].peFlags);
7764 for (i = 0; i < sizeof(test_data) / sizeof(*test_data); i++)
7766 memset(&surface_desc, 0, sizeof(surface_desc));
7767 surface_desc.dwSize = sizeof(surface_desc);
7768 surface_desc.dwFlags = DDSD_CAPS | test_data[i].flags;
7769 surface_desc.dwWidth = 128;
7770 surface_desc.dwHeight = 128;
7771 surface_desc.ddsCaps.dwCaps = test_data[i].caps;
7772 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7773 ok(SUCCEEDED(hr), "Failed to create %s surface, hr %#x.\n", test_data[i].name, hr);
7775 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7776 if (test_data[i].attach_allowed)
7777 ok(SUCCEEDED(hr), "Failed to attach palette to %s surface, hr %#x.\n", test_data[i].name, hr);
7778 else
7779 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x, %s surface.\n", hr, test_data[i].name);
7781 if (SUCCEEDED(hr))
7783 HDC dc;
7784 RGBQUAD rgbquad;
7785 UINT retval;
7787 hr = IDirectDrawSurface4_GetDC(surface, &dc);
7788 ok(SUCCEEDED(hr), "Failed to get DC, hr %#x, %s surface.\n", hr, test_data[i].name);
7789 retval = GetDIBColorTable(dc, 1, 1, &rgbquad);
7790 ok(retval == 1, "GetDIBColorTable returned unexpected result %u.\n", retval);
7791 ok(rgbquad.rgbRed == 0xff, "Expected rgbRed = 0xff, got %#x, %s surface.\n",
7792 rgbquad.rgbRed, test_data[i].name);
7793 ok(rgbquad.rgbGreen == 0, "Expected rgbGreen = 0, got %#x, %s surface.\n",
7794 rgbquad.rgbGreen, test_data[i].name);
7795 ok(rgbquad.rgbBlue == 0, "Expected rgbBlue = 0, got %#x, %s surface.\n",
7796 rgbquad.rgbBlue, test_data[i].name);
7797 todo_wine ok(rgbquad.rgbReserved == 0, "Expected rgbReserved = 0, got %u, %s surface.\n",
7798 rgbquad.rgbReserved, test_data[i].name);
7799 hr = IDirectDrawSurface4_ReleaseDC(surface, dc);
7800 ok(SUCCEEDED(hr), "Failed to release DC, hr %#x.\n", hr);
7802 IDirectDrawSurface4_Release(surface);
7805 /* Test INVALIDSURFACETYPE vs INVALIDPIXELFORMAT. */
7806 memset(&surface_desc, 0, sizeof(surface_desc));
7807 surface_desc.dwSize = sizeof(surface_desc);
7808 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
7809 surface_desc.dwWidth = 128;
7810 surface_desc.dwHeight = 128;
7811 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
7812 U4(surface_desc).ddpfPixelFormat.dwSize = sizeof(U4(surface_desc).ddpfPixelFormat);
7813 U4(surface_desc).ddpfPixelFormat.dwFlags = DDPF_RGB;
7814 U1(U4(surface_desc).ddpfPixelFormat).dwRGBBitCount = 32;
7815 U2(U4(surface_desc).ddpfPixelFormat).dwRBitMask = 0x00ff0000;
7816 U3(U4(surface_desc).ddpfPixelFormat).dwGBitMask = 0x0000ff00;
7817 U4(U4(surface_desc).ddpfPixelFormat).dwBBitMask = 0x000000ff;
7818 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7819 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7820 hr = IDirectDrawSurface4_SetPalette(surface, palette);
7821 ok(hr == DDERR_INVALIDSURFACETYPE, "Got unexpected hr %#x.\n", hr);
7822 IDirectDrawSurface4_Release(surface);
7824 /* The Windows 8 testbot keeps extra references to the primary
7825 * while in 8 bpp mode. */
7826 hr = IDirectDraw4_RestoreDisplayMode(ddraw);
7827 ok(SUCCEEDED(hr), "Failed to restore display mode, hr %#x.\n", hr);
7829 refcount = IDirectDrawPalette_Release(palette);
7830 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7831 refcount = IDirectDraw4_Release(ddraw);
7832 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
7833 DestroyWindow(window);
7836 static void test_vb_writeonly(void)
7838 IDirect3DDevice3 *device;
7839 IDirect3D3 *d3d;
7840 IDirect3DVertexBuffer *buffer;
7841 HWND window;
7842 HRESULT hr;
7843 D3DVERTEXBUFFERDESC desc;
7844 void *ptr;
7845 static const struct vec4 quad[] =
7847 { 0.0f, 480.0f, 0.0f, 1.0f},
7848 { 0.0f, 0.0f, 0.0f, 1.0f},
7849 {640.0f, 480.0f, 0.0f, 1.0f},
7850 {640.0f, 0.0f, 0.0f, 1.0f},
7853 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7854 0, 0, 640, 480, 0, 0, 0, 0);
7856 if (!(device = create_device(window, DDSCL_NORMAL)))
7858 skip("Failed to create a 3D device, skipping test.\n");
7859 DestroyWindow(window);
7860 return;
7863 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
7864 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
7866 memset(&desc, 0, sizeof(desc));
7867 desc.dwSize = sizeof(desc);
7868 desc.dwCaps = D3DVBCAPS_WRITEONLY;
7869 desc.dwFVF = D3DFVF_XYZRHW;
7870 desc.dwNumVertices = sizeof(quad) / sizeof(*quad);
7871 hr = IDirect3D3_CreateVertexBuffer(d3d, &desc, &buffer, 0, NULL);
7872 ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr);
7874 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_DISCARDCONTENTS, &ptr, NULL);
7875 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
7876 memcpy(ptr, quad, sizeof(quad));
7877 hr = IDirect3DVertexBuffer_Unlock(buffer);
7878 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
7880 hr = IDirect3DDevice3_BeginScene(device);
7881 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
7882 hr = IDirect3DDevice3_DrawPrimitiveVB(device, D3DPT_TRIANGLESTRIP, buffer, 0, 4, 0);
7883 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
7884 hr = IDirect3DDevice3_EndScene(device);
7885 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
7887 hr = IDirect3DVertexBuffer_Lock(buffer, 0, &ptr, NULL);
7888 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
7889 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
7890 hr = IDirect3DVertexBuffer_Unlock(buffer);
7891 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
7893 hr = IDirect3DVertexBuffer_Lock(buffer, DDLOCK_READONLY, &ptr, NULL);
7894 ok(SUCCEEDED(hr), "Failed to lock vertex buffer, hr %#x.\n", hr);
7895 ok (!memcmp(ptr, quad, sizeof(quad)), "Got unexpected vertex buffer data.\n");
7896 hr = IDirect3DVertexBuffer_Unlock(buffer);
7897 ok(SUCCEEDED(hr), "Failed to unlock vertex buffer, hr %#x.\n", hr);
7899 IDirect3DVertexBuffer_Release(buffer);
7900 IDirect3D3_Release(d3d);
7901 IDirect3DDevice3_Release(device);
7902 DestroyWindow(window);
7905 static void test_lost_device(void)
7907 IDirectDrawSurface4 *surface;
7908 DDSURFACEDESC2 surface_desc;
7909 IDirectDraw4 *ddraw;
7910 ULONG refcount;
7911 HWND window;
7912 HRESULT hr;
7913 BOOL ret;
7915 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
7916 0, 0, 640, 480, 0, 0, 0, 0);
7917 ddraw = create_ddraw();
7918 ok(!!ddraw, "Failed to create a ddraw object.\n");
7919 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
7920 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
7922 memset(&surface_desc, 0, sizeof(surface_desc));
7923 surface_desc.dwSize = sizeof(surface_desc);
7924 surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
7925 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
7926 U5(surface_desc).dwBackBufferCount = 1;
7927 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7928 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7930 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7931 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7932 hr = IDirectDrawSurface4_IsLost(surface);
7933 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7934 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
7935 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7937 ret = SetForegroundWindow(GetDesktopWindow());
7938 ok(ret, "Failed to set foreground window.\n");
7939 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7940 ok(hr == DDERR_NOEXCLUSIVEMODE, "Got unexpected hr %#x.\n", hr);
7941 hr = IDirectDrawSurface4_IsLost(surface);
7942 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7943 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
7944 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7946 ret = SetForegroundWindow(window);
7947 ok(ret, "Failed to set foreground window.\n");
7948 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7949 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7950 hr = IDirectDrawSurface4_IsLost(surface);
7951 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7952 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
7953 todo_wine ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7955 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
7956 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7957 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7958 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7959 hr = IDirectDrawSurface4_IsLost(surface);
7960 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7961 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
7962 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7964 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
7965 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7966 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7967 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7968 hr = IDirectDrawSurface4_IsLost(surface);
7969 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7970 hr = IDirectDrawSurface4_Flip(surface, NULL, DDFLIP_WAIT);
7971 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
7973 /* Trying to restore the primary will crash, probably because flippable
7974 * surfaces can't exist in DDSCL_NORMAL. */
7975 IDirectDrawSurface4_Release(surface);
7976 memset(&surface_desc, 0, sizeof(surface_desc));
7977 surface_desc.dwSize = sizeof(surface_desc);
7978 surface_desc.dwFlags = DDSD_CAPS;
7979 surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
7980 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
7981 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
7983 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7984 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7985 hr = IDirectDrawSurface4_IsLost(surface);
7986 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7988 ret = SetForegroundWindow(GetDesktopWindow());
7989 ok(ret, "Failed to set foreground window.\n");
7990 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7991 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7992 hr = IDirectDrawSurface4_IsLost(surface);
7993 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7995 ret = SetForegroundWindow(window);
7996 ok(ret, "Failed to set foreground window.\n");
7997 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
7998 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
7999 hr = IDirectDrawSurface4_IsLost(surface);
8000 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8002 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
8003 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8004 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8005 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8006 hr = IDirectDrawSurface4_IsLost(surface);
8007 ok(hr == DDERR_SURFACELOST, "Got unexpected hr %#x.\n", hr);
8009 hr = IDirectDraw4_RestoreAllSurfaces(ddraw);
8010 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8011 hr = IDirectDraw4_TestCooperativeLevel(ddraw);
8012 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8013 hr = IDirectDrawSurface4_IsLost(surface);
8014 ok(hr == DD_OK, "Got unexpected hr %#x.\n", hr);
8016 IDirectDrawSurface4_Release(surface);
8017 refcount = IDirectDraw4_Release(ddraw);
8018 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8019 DestroyWindow(window);
8022 static void test_surface_desc_lock(void)
8024 IDirectDrawSurface4 *surface;
8025 DDSURFACEDESC2 surface_desc;
8026 IDirectDraw4 *ddraw;
8027 ULONG refcount;
8028 HWND window;
8029 HRESULT hr;
8031 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8032 0, 0, 640, 480, 0, 0, 0, 0);
8033 ddraw = create_ddraw();
8034 ok(!!ddraw, "Failed to create a ddraw object.\n");
8035 hr = IDirectDraw4_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
8036 ok(SUCCEEDED(hr), "Failed to set cooperative level, hr %#x.\n", hr);
8038 memset(&surface_desc, 0, sizeof(surface_desc));
8039 surface_desc.dwSize = sizeof(surface_desc);
8040 surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
8041 surface_desc.dwWidth = 16;
8042 surface_desc.dwHeight = 16;
8043 surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
8044 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8045 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
8047 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8048 surface_desc.dwSize = sizeof(surface_desc);
8049 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8050 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8051 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8053 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8054 surface_desc.dwSize = sizeof(surface_desc);
8055 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
8056 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
8057 ok(surface_desc.lpSurface != NULL, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8058 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8059 surface_desc.dwSize = sizeof(surface_desc);
8060 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8061 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8062 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8063 hr = IDirectDrawSurface4_Unlock(surface, NULL);
8064 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
8066 memset(&surface_desc, 0xaa, sizeof(surface_desc));
8067 surface_desc.dwSize = sizeof(surface_desc);
8068 hr = IDirectDrawSurface4_GetSurfaceDesc(surface, &surface_desc);
8069 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8070 ok(!surface_desc.lpSurface, "Got unexpected lpSurface %p.\n", surface_desc.lpSurface);
8072 IDirectDrawSurface4_Release(surface);
8073 refcount = IDirectDraw4_Release(ddraw);
8074 ok(!refcount, "Got unexpected refcount %u.\n", refcount);
8075 DestroyWindow(window);
8078 static void test_signed_formats(void)
8080 HRESULT hr;
8081 IDirect3DDevice3 *device;
8082 IDirect3D3 *d3d;
8083 IDirectDraw4 *ddraw;
8084 IDirectDrawSurface4 *surface, *rt;
8085 IDirect3DTexture2 *texture;
8086 IDirect3DViewport3 *viewport;
8087 DDSURFACEDESC2 surface_desc;
8088 ULONG refcount;
8089 HWND window;
8090 D3DCOLOR color, expected_color;
8091 D3DRECT clear_rect;
8092 static struct
8094 struct vec3 position;
8095 struct vec2 texcoord;
8097 quad[] =
8099 {{-1.0f, -1.0f, 0.0f}, {0.0f, 1.0f}},
8100 {{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f}},
8101 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 1.0f}},
8102 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
8104 /* See test_signed_formats() in dlls/d3d9/tests/visual.c for an explanation
8105 * of these values. */
8106 static const USHORT content_v8u8[4][4] =
8108 {0x0000, 0x7f7f, 0x8880, 0x0000},
8109 {0x0080, 0x8000, 0x7f00, 0x007f},
8110 {0x193b, 0xe8c8, 0x0808, 0xf8f8},
8111 {0x4444, 0xc0c0, 0xa066, 0x22e0},
8113 static const DWORD content_x8l8v8u8[4][4] =
8115 {0x00000000, 0x00ff7f7f, 0x00008880, 0x00ff0000},
8116 {0x00000080, 0x00008000, 0x00007f00, 0x0000007f},
8117 {0x0041193b, 0x0051e8c8, 0x00040808, 0x00fff8f8},
8118 {0x00824444, 0x0000c0c0, 0x00c2a066, 0x009222e0},
8120 static const USHORT content_l6v5u5[4][4] =
8122 {0x0000, 0xfdef, 0x0230, 0xfc00},
8123 {0x0010, 0x0200, 0x01e0, 0x000f},
8124 {0x4067, 0x53b9, 0x0421, 0xffff},
8125 {0x8108, 0x0318, 0xc28c, 0x909c},
8127 static const struct
8129 const char *name;
8130 const void *content;
8131 SIZE_T pixel_size;
8132 BOOL blue;
8133 unsigned int slop, slop_broken;
8134 DDPIXELFORMAT format;
8136 formats[] =
8139 "D3DFMT_V8U8", content_v8u8, sizeof(WORD), FALSE, 1, 0,
8141 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV, 0,
8142 {16}, {0x000000ff}, {0x0000ff00}, {0x00000000}, {0x00000000}
8146 "D3DFMT_X8L8V8U8", content_x8l8v8u8, sizeof(DWORD), TRUE, 1, 0,
8148 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV | DDPF_BUMPLUMINANCE, 0,
8149 {32}, {0x000000ff}, {0x0000ff00}, {0x00ff0000}, {0x00000000}
8153 "D3DFMT_L6V5U5", content_l6v5u5, sizeof(WORD), TRUE, 4, 7,
8155 sizeof(DDPIXELFORMAT), DDPF_BUMPDUDV | DDPF_BUMPLUMINANCE, 0,
8156 {16}, {0x0000001f}, {0x000003e0}, {0x0000fc00}, {0x00000000}
8160 /* No V16U16 or Q8W8V8U8 support in ddraw. */
8162 static const D3DCOLOR expected_colors[4][4] =
8164 {0x00808080, 0x00fefeff, 0x00010780, 0x008080ff},
8165 {0x00018080, 0x00800180, 0x0080fe80, 0x00fe8080},
8166 {0x00ba98a0, 0x004767a8, 0x00888881, 0x007878ff},
8167 {0x00c3c3c0, 0x003f3f80, 0x00e51fe1, 0x005fa2c8},
8169 unsigned int i, width, x, y;
8170 D3DDEVICEDESC device_desc, hel_desc;
8172 window = CreateWindowA("static", "ddraw_test", WS_OVERLAPPEDWINDOW,
8173 0, 0, 640, 480, 0, 0, 0, 0);
8175 if (!(device = create_device(window, DDSCL_NORMAL)))
8177 skip("Failed to create a 3D device, skipping test.\n");
8178 DestroyWindow(window);
8179 return;
8182 memset(&device_desc, 0, sizeof(device_desc));
8183 device_desc.dwSize = sizeof(device_desc);
8184 memset(&hel_desc, 0, sizeof(hel_desc));
8185 hel_desc.dwSize = sizeof(hel_desc);
8186 hr = IDirect3DDevice3_GetCaps(device, &device_desc, &hel_desc);
8187 ok(SUCCEEDED(hr), "Failed to get device caps, hr %#x.\n", hr);
8188 if (!(device_desc.dwTextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA))
8190 skip("D3DTOP_BLENDFACTORALPHA not supported, skipping bumpmap format tests.\n");
8191 goto done;
8194 hr = IDirect3DDevice3_GetDirect3D(device, &d3d);
8195 ok(SUCCEEDED(hr), "Failed to get d3d interface, hr %#x.\n", hr);
8196 hr = IDirect3D3_QueryInterface(d3d, &IID_IDirectDraw4, (void **)&ddraw);
8197 ok(SUCCEEDED(hr), "Failed to get ddraw interface, hr %#x.\n", hr);
8198 hr = IDirect3DDevice3_GetRenderTarget(device, &rt);
8199 ok(SUCCEEDED(hr), "Failed to get render target, hr %#x.\n", hr);
8201 memset(&surface_desc, 0, sizeof(surface_desc));
8202 surface_desc.dwSize = sizeof(surface_desc);
8203 hr = IDirectDrawSurface4_GetSurfaceDesc(rt, &surface_desc);
8204 ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
8205 viewport = create_viewport(device, 0, 0, surface_desc.dwWidth, surface_desc.dwHeight);
8206 hr = IDirect3DDevice3_SetCurrentViewport(device, viewport);
8207 ok(SUCCEEDED(hr), "Failed to activate the viewport, hr %#x.\n", hr);
8208 U1(clear_rect).x1 = 0;
8209 U2(clear_rect).y1 = 0;
8210 U3(clear_rect).x2 = surface_desc.dwWidth;
8211 U4(clear_rect).y2 = surface_desc.dwHeight;
8213 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
8214 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8216 /* dst = tex * 0.5 + 1.0 * (1.0 - 0.5) = tex * 0.5 + 0.5 */
8217 hr = IDirect3DDevice3_SetRenderState(device, D3DRENDERSTATE_TEXTUREFACTOR, 0x80ffffff);
8218 ok(SUCCEEDED(hr), "Failed to set render state, hr %#x.\n", hr);
8219 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLOROP, D3DTOP_BLENDFACTORALPHA);
8220 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
8221 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
8222 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
8223 hr = IDirect3DDevice3_SetTextureStageState(device, 0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
8224 ok(SUCCEEDED(hr), "Failed to set texture stage state, hr %#x.\n", hr);
8226 for (i = 0; i < sizeof(formats) / sizeof(*formats); i++)
8228 for (width = 1; width < 5; width += 3)
8230 hr = IDirect3DViewport3_Clear2(viewport, 1, &clear_rect, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
8231 ok(SUCCEEDED(hr), "Failed to clear viewport, hr %#x.\n", hr);
8233 memset(&surface_desc, 0, sizeof(surface_desc));
8234 surface_desc.dwSize = sizeof(surface_desc);
8235 surface_desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
8236 surface_desc.dwWidth = width;
8237 surface_desc.dwHeight = 4;
8238 U4(surface_desc).ddpfPixelFormat = formats[i].format;
8239 surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
8240 hr = IDirectDraw4_CreateSurface(ddraw, &surface_desc, &surface, NULL);
8241 if (FAILED(hr))
8243 skip("%s textures not supported, skipping.\n", formats[i].name);
8244 continue;
8246 ok(SUCCEEDED(hr), "Failed to create surface, hr %#x, format %s.\n", hr, formats[i].name);
8248 hr = IDirectDrawSurface4_QueryInterface(surface, &IID_IDirect3DTexture2, (void **)&texture);
8249 ok(SUCCEEDED(hr), "Failed to get Direct3DTexture2 interface, hr %#x, format %s.\n",
8250 hr, formats[i].name);
8251 hr = IDirect3DDevice3_SetTexture(device, 0, texture);
8252 ok(SUCCEEDED(hr), "Failed to set texture, hr %#x, format %s.\n", hr, formats[i].name);
8253 IDirect3DTexture2_Release(texture);
8255 memset(&surface_desc, 0, sizeof(surface_desc));
8256 surface_desc.dwSize = sizeof(surface_desc);
8257 hr = IDirectDrawSurface4_Lock(surface, NULL, &surface_desc, 0, NULL);
8258 ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x, format %s.\n", hr, formats[i].name);
8259 for (y = 0; y < 4; y++)
8261 memcpy((char *)surface_desc.lpSurface + y * surface_desc.lPitch,
8262 (char *)formats[i].content + y * 4 * formats[i].pixel_size,
8263 width * formats[i].pixel_size);
8265 hr = IDirectDrawSurface4_Unlock(surface, NULL);
8266 ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x, format %s.\n", hr, formats[i].name);
8268 hr = IDirect3DDevice3_BeginScene(device);
8269 ok(SUCCEEDED(hr), "Failed to begin scene, hr %#x.\n", hr);
8270 hr = IDirect3DDevice3_DrawPrimitive(device, D3DPT_TRIANGLESTRIP,
8271 D3DFVF_XYZ | D3DFVF_TEX1, quad, 4, 0);
8272 ok(SUCCEEDED(hr), "Failed to draw, hr %#x.\n", hr);
8273 hr = IDirect3DDevice3_EndScene(device);
8274 ok(SUCCEEDED(hr), "Failed to end scene, hr %#x.\n", hr);
8276 for (y = 0; y < 4; y++)
8278 for (x = 0; x < width; x++)
8280 expected_color = expected_colors[y][x];
8281 if (!formats[i].blue)
8282 expected_color |= 0x000000ff;
8284 color = get_surface_color(rt, 80 + 160 * x, 60 + 120 * y);
8285 ok(compare_color(color, expected_color, formats[i].slop)
8286 || broken(compare_color(color, expected_color, formats[i].slop_broken)),
8287 "Expected color 0x%08x, got 0x%08x, format %s, location %ux%u.\n",
8288 expected_color, color, formats[i].name, x, y);
8292 IDirectDrawSurface4_Release(surface);
8296 destroy_viewport(device, viewport);
8297 IDirectDrawSurface4_Release(rt);
8298 IDirectDraw4_Release(ddraw);
8299 IDirect3D3_Release(d3d);
8301 done:
8302 refcount = IDirect3DDevice3_Release(device);
8303 ok(!refcount, "Device has %u references left.\n", refcount);
8304 DestroyWindow(window);
8307 START_TEST(ddraw4)
8309 IDirectDraw4 *ddraw;
8310 DEVMODEW current_mode;
8312 if (!(ddraw = create_ddraw()))
8314 skip("Failed to create a ddraw object, skipping tests.\n");
8315 return;
8317 IDirectDraw4_Release(ddraw);
8319 memset(&current_mode, 0, sizeof(current_mode));
8320 current_mode.dmSize = sizeof(current_mode);
8321 ok(EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &current_mode), "Failed to get display mode.\n");
8322 registry_mode.dmSize = sizeof(registry_mode);
8323 ok(EnumDisplaySettingsW(NULL, ENUM_REGISTRY_SETTINGS, &registry_mode), "Failed to get display mode.\n");
8324 if (registry_mode.dmPelsWidth != current_mode.dmPelsWidth
8325 || registry_mode.dmPelsHeight != current_mode.dmPelsHeight)
8327 skip("Current mode does not match registry mode, skipping test.\n");
8328 return;
8331 test_process_vertices();
8332 test_coop_level_create_device_window();
8333 test_clipper_blt();
8334 test_coop_level_d3d_state();
8335 test_surface_interface_mismatch();
8336 test_coop_level_threaded();
8337 test_depth_blit();
8338 test_texture_load_ckey();
8339 test_viewport();
8340 test_zenable();
8341 test_ck_rgba();
8342 test_ck_default();
8343 test_ck_complex();
8344 test_surface_qi();
8345 test_device_qi();
8346 test_wndproc();
8347 test_window_style();
8348 test_redundant_mode_set();
8349 test_coop_level_mode_set();
8350 test_coop_level_mode_set_multi();
8351 test_initialize();
8352 test_coop_level_surf_create();
8353 test_vb_discard();
8354 test_coop_level_multi_window();
8355 test_draw_strided();
8356 test_clear_rect_count();
8357 test_coop_level_versions();
8358 test_lighting_interface_versions();
8359 test_coop_level_activateapp();
8360 test_texturemanage();
8361 test_block_formats_creation();
8362 test_unsupported_formats();
8363 test_rt_caps();
8364 test_primary_caps();
8365 test_surface_lock();
8366 test_surface_discard();
8367 test_flip();
8368 test_set_surface_desc();
8369 test_user_memory_getdc();
8370 test_sysmem_overlay();
8371 test_primary_palette();
8372 test_surface_attachment();
8373 test_private_data();
8374 test_pixel_format();
8375 test_create_surface_pitch();
8376 test_mipmap_lock();
8377 test_palette_complex();
8378 test_p8_rgb_blit();
8379 test_material();
8380 test_palette_gdi();
8381 test_palette_alpha();
8382 test_vb_writeonly();
8383 test_lost_device();
8384 test_surface_desc_lock();
8385 test_signed_formats();