push 6434d21a91ee1ef2b42b81e2beea154c98f99170
[wine/hacks.git] / dlls / d3d8 / tests / surface.c
blob6e0d48c2bc4478f84032dd675aed4811a7e6f4b6
1 /*
2 * Copyright 2006-2007 Henri Verbeet
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 #define COBJMACROS
19 #include <d3d8.h>
20 #include "wine/test.h"
22 static HWND create_window(void)
24 WNDCLASS wc = {0};
25 wc.lpfnWndProc = DefWindowProc;
26 wc.lpszClassName = "d3d8_test_wc";
27 RegisterClass(&wc);
29 return CreateWindow("d3d8_test_wc", "d3d8_test",
30 0, 0, 0, 0, 0, 0, 0, 0, 0);
33 static IDirect3DDevice8 *init_d3d8(HMODULE d3d8_handle)
35 IDirect3D8 * (__stdcall * d3d8_create)(UINT SDKVersion) = 0;
36 IDirect3D8 *d3d8_ptr = 0;
37 IDirect3DDevice8 *device_ptr = 0;
38 D3DPRESENT_PARAMETERS present_parameters;
39 D3DDISPLAYMODE d3ddm;
40 HRESULT hr;
42 d3d8_create = (void *)GetProcAddress(d3d8_handle, "Direct3DCreate8");
43 ok(d3d8_create != NULL, "Failed to get address of Direct3DCreate8\n");
44 if (!d3d8_create) return NULL;
46 d3d8_ptr = d3d8_create(D3D_SDK_VERSION);
47 ok(d3d8_ptr != NULL, "Failed to create IDirect3D8 object\n");
48 if (!d3d8_ptr) return NULL;
50 IDirect3D8_GetAdapterDisplayMode(d3d8_ptr, D3DADAPTER_DEFAULT, &d3ddm );
51 ZeroMemory(&present_parameters, sizeof(present_parameters));
52 present_parameters.Windowed = TRUE;
53 present_parameters.hDeviceWindow = create_window();
54 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
55 present_parameters.BackBufferFormat = d3ddm.Format;
57 hr = IDirect3D8_CreateDevice(d3d8_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
58 NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
60 if(FAILED(hr))
62 skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
63 return NULL;
66 return device_ptr;
69 static void test_surface_get_container(IDirect3DDevice8 *device_ptr)
71 IDirect3DTexture8 *texture_ptr = 0;
72 IDirect3DSurface8 *surface_ptr = 0;
73 void *container_ptr;
74 HRESULT hr;
76 hr = IDirect3DDevice8_CreateTexture(device_ptr, 128, 128, 1, 0,
77 D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture_ptr);
78 ok(SUCCEEDED(hr) && texture_ptr != NULL, "CreateTexture returned: hr %#x, texture_ptr %p. "
79 "Expected hr %#x, texture_ptr != %p\n", hr, texture_ptr, D3D_OK, NULL);
80 if (!texture_ptr || FAILED(hr)) goto cleanup;
82 hr = IDirect3DTexture8_GetSurfaceLevel(texture_ptr, 0, &surface_ptr);
83 ok(SUCCEEDED(hr) && surface_ptr != NULL, "GetSurfaceLevel returned: hr %#x, surface_ptr %p. "
84 "Expected hr %#x, surface_ptr != %p\n", hr, surface_ptr, D3D_OK, NULL);
85 if (!surface_ptr || FAILED(hr)) goto cleanup;
87 /* These should work... */
88 container_ptr = (void *)0x1337c0d3;
89 hr = IDirect3DSurface8_GetContainer(surface_ptr, &IID_IUnknown, &container_ptr);
90 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
91 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
92 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
94 container_ptr = (void *)0x1337c0d3;
95 hr = IDirect3DSurface8_GetContainer(surface_ptr, &IID_IDirect3DResource8, &container_ptr);
96 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
97 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
98 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
100 container_ptr = (void *)0x1337c0d3;
101 hr = IDirect3DSurface8_GetContainer(surface_ptr, &IID_IDirect3DBaseTexture8, &container_ptr);
102 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
103 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
104 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
106 container_ptr = (void *)0x1337c0d3;
107 hr = IDirect3DSurface8_GetContainer(surface_ptr, &IID_IDirect3DTexture8, &container_ptr);
108 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
109 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
110 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
112 /* ...and this one shouldn't. This should return E_NOINTERFACE and set container_ptr to NULL */
113 container_ptr = (void *)0x1337c0d3;
114 hr = IDirect3DSurface8_GetContainer(surface_ptr, &IID_IDirect3DSurface8, &container_ptr);
115 ok(hr == E_NOINTERFACE && container_ptr == NULL, "GetContainer returned: hr %#x, container_ptr %p. "
116 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, E_NOINTERFACE, NULL);
117 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
119 cleanup:
120 if (texture_ptr) IDirect3DTexture8_Release(texture_ptr);
121 if (surface_ptr) IDirect3DSurface8_Release(surface_ptr);
124 static void test_lockrect_invalid(IDirect3DDevice8 *device)
126 IDirect3DSurface8 *surface = 0;
127 D3DLOCKED_RECT locked_rect;
128 unsigned int i;
129 BYTE *base;
130 HRESULT hr;
132 const RECT valid[] = {
133 {60, 60, 68, 68},
134 {120, 60, 128, 68},
135 {60, 120, 68, 128},
138 const RECT invalid[] = {
139 {60, 60, 60, 68}, /* 0 height */
140 {60, 60, 68, 60}, /* 0 width */
141 {68, 60, 60, 68}, /* left > right */
142 {60, 68, 68, 60}, /* top > bottom */
143 {-8, 60, 0, 68}, /* left < surface */
144 {60, -8, 68, 0}, /* top < surface */
145 {-16, 60, -8, 68}, /* right < surface */
146 {60, -16, 68, -8}, /* bottom < surface */
147 {60, 60, 136, 68}, /* right > surface */
148 {60, 60, 68, 136}, /* bottom > surface */
149 {136, 60, 144, 68}, /* left > surface */
150 {60, 136, 68, 144}, /* top > surface */
153 hr = IDirect3DDevice8_CreateImageSurface(device, 128, 128, D3DFMT_A8R8G8B8, &surface);
154 ok(SUCCEEDED(hr), "CreateImageSurface failed (0x%08x)\n", hr);
156 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, NULL, 0);
157 ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
159 base = locked_rect.pBits;
161 hr = IDirect3DSurface8_UnlockRect(surface);
162 ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
164 for (i = 0; i < (sizeof(valid) / sizeof(*valid)); ++i)
166 unsigned int offset, expected_offset;
167 const RECT *rect = &valid[i];
169 locked_rect.pBits = (BYTE *)0xdeadbeef;
170 locked_rect.Pitch = 0xdeadbeef;
172 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, rect, 0);
173 ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
174 hr, rect->left, rect->top, rect->right, rect->bottom);
176 offset = (BYTE *)locked_rect.pBits - base;
177 expected_offset = rect->top * locked_rect.Pitch + rect->left * 4;
178 ok(offset == expected_offset, "Got offset %u, expected offset %u for rect [%d, %d]->[%d, %d]\n",
179 offset, expected_offset, rect->left, rect->top, rect->right, rect->bottom);
181 hr = IDirect3DSurface8_UnlockRect(surface);
182 ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
185 for (i = 0; i < (sizeof(invalid) / sizeof(*invalid)); ++i)
187 const RECT *rect = &invalid[i];
189 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, rect, 0);
190 ok(hr == D3DERR_INVALIDCALL, "LockRect returned 0x%08x for rect [%d, %d]->[%d, %d]"
191 ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, rect->left, rect->top,
192 rect->right, rect->bottom, D3DERR_INVALIDCALL);
195 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, NULL, 0);
196 ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect NULL\n", hr);
197 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, NULL, 0);
198 ok(hr == D3DERR_INVALIDCALL, "Double LockRect returned 0x%08x for rect NULL\n", hr);
199 hr = IDirect3DSurface8_UnlockRect(surface);
200 ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
202 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, &valid[0], 0);
203 ok(hr == D3D_OK, "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
204 ", expected D3D_OK (0x%08x)\n", hr, valid[0].left, valid[0].top,
205 valid[0].right, valid[0].bottom, D3D_OK);
206 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, &valid[0], 0);
207 ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
208 ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, valid[0].left, valid[0].top,
209 valid[0].right, valid[0].bottom,D3DERR_INVALIDCALL);
210 hr = IDirect3DSurface8_LockRect(surface, &locked_rect, &valid[1], 0);
211 ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
212 ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, valid[1].left, valid[1].top,
213 valid[1].right, valid[1].bottom, D3DERR_INVALIDCALL);
214 hr = IDirect3DSurface8_UnlockRect(surface);
215 ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
217 IDirect3DSurface8_Release(surface);
220 static unsigned long getref(IUnknown *iface)
222 IUnknown_AddRef(iface);
223 return IUnknown_Release(iface);
226 static void test_private_data(IDirect3DDevice8 *device)
228 HRESULT hr;
229 IDirect3DSurface8 *surface;
230 ULONG ref, ref2;
231 IUnknown *ptr;
232 DWORD size = sizeof(IUnknown *);
234 hr = IDirect3DDevice8_CreateImageSurface(device, 4, 4, D3DFMT_A8R8G8B8, &surface);
235 ok(SUCCEEDED(hr), "CreateImageSurface failed (0x%08x)\n", hr);
236 if(!surface)
238 return;
241 /* This fails */
242 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8 /* Abuse this tag */, device, 0, D3DSPD_IUNKNOWN);
243 ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
244 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8 /* Abuse this tag */, device, 5, D3DSPD_IUNKNOWN);
245 ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
246 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8 /* Abuse this tag */, device, sizeof(IUnknown *) * 2, D3DSPD_IUNKNOWN);
247 ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
249 ref = getref((IUnknown *) device);
250 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8 /* Abuse this tag */, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
251 ok(hr == D3D_OK, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
252 ref2 = getref((IUnknown *) device);
253 ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
254 hr = IDirect3DSurface8_FreePrivateData(surface, &IID_IDirect3DSurface8);
255 ok(hr == D3D_OK, "IDirect3DSurface8_FreePrivateData returned %08x\n", hr);
256 ref2 = getref((IUnknown *) device);
257 ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
259 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
260 ok(hr == D3D_OK, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
261 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8, surface, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
262 ok(hr == D3D_OK, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
263 ref2 = getref((IUnknown *) device);
264 ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
266 hr = IDirect3DSurface8_SetPrivateData(surface, &IID_IDirect3DSurface8, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
267 ok(hr == D3D_OK, "IDirect3DSurface8_SetPrivateData failed with %08x\n", hr);
268 hr = IDirect3DSurface8_GetPrivateData(surface, &IID_IDirect3DSurface8, &ptr, &size);
269 ok(hr == D3D_OK, "IDirect3DSurface8_GetPrivateData failed with %08x\n", hr);
270 ref2 = getref((IUnknown *) device);
271 /* Object is NOT being addrefed */
272 ok(ptr == (IUnknown *) device, "Returned interface pointer is %p, expected %p\n", ptr, device);
273 ok(ref2 == ref + 2, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 2, ptr, device);
274 IUnknown_Release(ptr);
276 IDirect3DSurface8_Release(surface);
278 /* Destroying the surface frees the held reference */
279 ref2 = getref((IUnknown *) device);
280 /* -1 because the surface was released and held a reference before */
281 ok(ref2 == (ref - 1), "Object reference is %d, expected %d\n", ref2, ref - 1);
284 START_TEST(surface)
286 HMODULE d3d8_handle;
287 IDirect3DDevice8 *device_ptr;
289 d3d8_handle = LoadLibraryA("d3d8.dll");
290 if (!d3d8_handle)
292 skip("Could not load d3d8.dll\n");
293 return;
296 device_ptr = init_d3d8(d3d8_handle);
297 if (!device_ptr) return;
299 test_surface_get_container(device_ptr);
300 test_lockrect_invalid(device_ptr);
301 test_private_data(device_ptr);