d3d9/tests: Add a test for clearing render targets in an MRT.
[wine] / dlls / d3d9 / tests / surface.c
1 /*
2  * Copyright 2006-2007 Henri Verbeet
3  *
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.
8  *
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.
13  *
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
17  */
18 #define COBJMACROS
19 #include <d3d9.h>
20 #include "wine/test.h"
21
22 static HWND create_window(void)
23 {
24     WNDCLASS wc = {0};
25     wc.lpfnWndProc = DefWindowProc;
26     wc.lpszClassName = "d3d9_test_wc";
27     RegisterClass(&wc);
28
29     return CreateWindow("d3d9_test_wc", "d3d9_test",
30             0, 0, 0, 0, 0, 0, 0, 0, 0);
31 }
32
33 static IDirect3DDevice9 *init_d3d9(HMODULE d3d9_handle)
34 {
35     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
36     IDirect3D9 *d3d9_ptr = 0;
37     IDirect3DDevice9 *device_ptr = 0;
38     D3DPRESENT_PARAMETERS present_parameters;
39     HRESULT hr;
40
41     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
42     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
43     if (!d3d9_create) return NULL;
44
45     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
46     if (!d3d9_ptr)
47     {
48         skip("could not create D3D9\n");
49         return NULL;
50     }
51
52     ZeroMemory(&present_parameters, sizeof(present_parameters));
53     present_parameters.Windowed = TRUE;
54     present_parameters.hDeviceWindow = create_window();
55     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
56
57     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
58             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
59
60     if(FAILED(hr))
61     {
62         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
63         return NULL;
64     }
65
66     return device_ptr;
67 }
68
69 static void test_surface_get_container(IDirect3DDevice9 *device_ptr)
70 {
71     IDirect3DTexture9 *texture_ptr = 0;
72     IDirect3DSurface9 *surface_ptr = 0;
73     void *container_ptr;
74     HRESULT hr;
75
76     hr = IDirect3DDevice9_CreateTexture(device_ptr, 128, 128, 1, 0,
77             D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture_ptr, 0);
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;
81
82     hr = IDirect3DTexture9_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;
86
87     /* These should work... */
88     container_ptr = (void *)0x1337c0d3;
89     hr = IDirect3DSurface9_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);
93
94     container_ptr = (void *)0x1337c0d3;
95     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DResource9, &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);
99
100     container_ptr = (void *)0x1337c0d3;
101     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DBaseTexture9, &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);
105
106     container_ptr = (void *)0x1337c0d3;
107     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DTexture9, &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);
111
112     /* ...and this one shouldn't. This should return E_NOINTERFACE and set container_ptr to NULL */
113     container_ptr = (void *)0x1337c0d3;
114     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DSurface9, &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);
118
119 cleanup:
120     if (texture_ptr) IDirect3DTexture9_Release(texture_ptr);
121     if (surface_ptr) IDirect3DSurface9_Release(surface_ptr);
122 }
123
124 static void test_surface_alignment(IDirect3DDevice9 *device_ptr)
125 {
126     IDirect3DSurface9 *surface_ptr = 0;
127     HRESULT hr;
128     int i;
129
130     /* Test a sysmem surface as those aren't affected by the hardware's np2 restrictions */
131     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device_ptr, 5, 5, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface_ptr, 0);
132     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
133
134     if(surface_ptr)
135     {
136         D3DLOCKED_RECT lockedRect;
137         hr = IDirect3DSurface9_LockRect(surface_ptr, &lockedRect, NULL, 0);
138         ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
139         ok(!(lockedRect.Pitch & 3), "Surface pitch %d is not 32-bit aligned\n", lockedRect.Pitch);
140         /* Some applications also depend on the exact pitch, rather than just
141          * the alignment.
142          */
143         ok(lockedRect.Pitch == 12, "Got pitch %d, expected 12\n", lockedRect.Pitch);
144         hr = IDirect3DSurface9_UnlockRect(surface_ptr);
145         ok(SUCCEEDED(hr), "IDirect3DSurface9_UnlockRect returned %#x.\n", hr);
146         IDirect3DSurface9_Release(surface_ptr);
147     }
148
149     for (i = 0; i < 5; i++)
150     {
151         IDirect3DTexture9 *pTexture;
152         int j, pitch;
153
154         hr = IDirect3DDevice9_CreateTexture(device_ptr, 64, 64, 0, 0, MAKEFOURCC('D', 'X', 'T', '1'+i),
155                                             D3DPOOL_MANAGED, &pTexture, NULL);
156         ok(SUCCEEDED(hr) || hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_CreateTexture: %08x\n", hr);
157         if (FAILED(hr)) {
158             skip("DXT%d surfaces are not supported\n", i + 1);
159             continue;
160         }
161
162         for (j = IDirect3DBaseTexture9_GetLevelCount(pTexture) - 1; j >= 0; j--)
163         {
164             D3DLOCKED_RECT rc;
165             D3DSURFACE_DESC descr;
166
167             IDirect3DTexture9_GetLevelDesc(pTexture, j, &descr);
168             hr = IDirect3DTexture9_LockRect(pTexture, j, &rc, NULL, 0);
169             ok(SUCCEEDED(hr), "IDirect3DTexture9_LockRect: %08x\n", hr);
170             hr = IDirect3DTexture9_UnlockRect(pTexture, j);
171             ok(SUCCEEDED(hr), "IDirect3DTexture9_UnLockRect: %08x\n", hr);
172             /* Windows XP returns D3D_OK when calling UnlockRect on an unlocked surface,
173              * windows 7 returns an error.
174              */
175
176             pitch = ((descr.Width + 3) >> 2) << 3;
177             if (i > 0) pitch <<= 1;
178             ok(rc.Pitch == pitch, "Wrong pitch for DXT%d lvl[%d (%dx%d)]: expected %d got %d\n",
179                i + 1, j, descr.Width, descr.Height, pitch, rc.Pitch);
180         }
181         IUnknown_Release( pTexture );
182     }
183 }
184
185 /* Since the DXT formats are based on 4x4 blocks, locking works slightly
186  * different than with regular formats. This patch verifies we return the
187  * correct memory offsets */
188 static void test_lockrect_offset(IDirect3DDevice9 *device)
189 {
190     IDirect3DSurface9 *surface = 0;
191     IDirect3D9 *d3d;
192     const RECT rect = {60, 60, 68, 68};
193     D3DLOCKED_RECT locked_rect;
194     int expected_pitch;
195     unsigned int expected_offset;
196     unsigned int offset;
197     unsigned int i;
198     BYTE *base;
199     HRESULT hr;
200
201     const struct {
202         D3DFORMAT fmt;
203         const char *name;
204         unsigned int block_width;
205         unsigned int block_height;
206         unsigned int block_size;
207     } dxt_formats[] = {
208         {D3DFMT_DXT1,                 "D3DFMT_DXT1", 4, 4, 8},
209         {D3DFMT_DXT2,                 "D3DFMT_DXT2", 4, 4, 16},
210         {D3DFMT_DXT3,                 "D3DFMT_DXT3", 4, 4, 16},
211         {D3DFMT_DXT4,                 "D3DFMT_DXT4", 4, 4, 16},
212         {D3DFMT_DXT5,                 "D3DFMT_DXT5", 4, 4, 16},
213         {MAKEFOURCC('A','T','I','2'), "ATI2N",       1, 1,  1},
214     };
215     hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
216     ok(SUCCEEDED(hr), "IDirect3DDevice9_GetDirect3D failed (%08x)\n", hr);
217
218     for (i = 0; i < (sizeof(dxt_formats) / sizeof(*dxt_formats)); ++i) {
219         hr = IDirect3D9_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0, D3DRTYPE_TEXTURE, dxt_formats[i].fmt);
220         if(FAILED(hr))
221         {
222             skip("Format %s not supported, skipping lockrect offset test\n", dxt_formats[i].name);
223             continue;
224         }
225
226         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, dxt_formats[i].fmt, D3DPOOL_SCRATCH, &surface, 0);
227         ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (%08x)\n", hr);
228
229         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
230         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
231
232         base = locked_rect.pBits;
233         expected_pitch = (128 + dxt_formats[i].block_height - 1) / dxt_formats[i].block_width
234                          * dxt_formats[i].block_size;
235         ok(locked_rect.Pitch == expected_pitch, "Got pitch %d, expected pitch %d for format %s\n", locked_rect.Pitch, expected_pitch, dxt_formats[i].name);
236
237         hr = IDirect3DSurface9_UnlockRect(surface);
238         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
239         hr = IDirect3DSurface9_UnlockRect(surface);
240         ok(hr == D3DERR_INVALIDCALL, "Double UnlockRect returned %08x, expected D3DERR_INVALIDCALL\n", hr);
241
242         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &rect, 0);
243         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
244
245         offset = (BYTE *)locked_rect.pBits - base;
246         expected_offset = (rect.top / dxt_formats[i].block_height) * expected_pitch
247                         + (rect.left / dxt_formats[i].block_width) * dxt_formats[i].block_size;
248         ok(offset == expected_offset, "Got offset %u, expected offset %u for format %s\n", offset, expected_offset, dxt_formats[i].name);
249
250         hr = IDirect3DSurface9_UnlockRect(surface);
251         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
252
253         IDirect3DSurface9_Release(surface);
254     }
255     IDirect3D9_Release(d3d);
256 }
257
258 struct rect_test
259 {
260     RECT rect;
261     HRESULT win7_result;
262 };
263
264 static void test_lockrect_invalid(IDirect3DDevice9 *device)
265 {
266     IDirect3DSurface9 *surface = 0;
267     D3DLOCKED_RECT locked_rect;
268     unsigned int i;
269     BYTE *base;
270     HRESULT hr;
271
272     const RECT test_rect_2 = { 0, 0, 8, 8 };
273     const struct rect_test test_data[] = {
274         {{60, 60, 68, 68},      D3D_OK},                /* Valid */
275         {{60, 60, 60, 68},      D3DERR_INVALIDCALL},    /* 0 height */
276         {{60, 60, 68, 60},      D3DERR_INVALIDCALL},    /* 0 width */
277         {{68, 60, 60, 68},      D3DERR_INVALIDCALL},    /* left > right */
278         {{60, 68, 68, 60},      D3DERR_INVALIDCALL},    /* top > bottom */
279         {{-8, 60,  0, 68},      D3DERR_INVALIDCALL},    /* left < surface */
280         {{60, -8, 68,  0},      D3DERR_INVALIDCALL},    /* top < surface */
281         {{-16, 60, -8, 68},     D3DERR_INVALIDCALL},    /* right < surface */
282         {{60, -16, 68, -8},     D3DERR_INVALIDCALL},    /* bottom < surface */
283         {{60, 60, 136, 68},     D3DERR_INVALIDCALL},    /* right > surface */
284         {{60, 60, 68, 136},     D3DERR_INVALIDCALL},    /* bottom > surface */
285         {{136, 60, 144, 68},    D3DERR_INVALIDCALL},    /* left > surface */
286         {{60, 136, 68, 144},    D3DERR_INVALIDCALL},    /* top > surface */
287     };
288
289     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
290     ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (0x%08x)\n", hr);
291
292     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
293     ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
294
295     base = locked_rect.pBits;
296
297     hr = IDirect3DSurface9_UnlockRect(surface);
298     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
299
300     for (i = 0; i < (sizeof(test_data) / sizeof(*test_data)); ++i)
301     {
302         unsigned int offset, expected_offset;
303         const RECT *rect = &test_data[i].rect;
304
305         locked_rect.pBits = (BYTE *)0xdeadbeef;
306         locked_rect.Pitch = 0xdeadbeef;
307
308         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, rect, 0);
309         /* Windows XP accepts invalid locking rectangles, windows 7 rejects them.
310          * Some games(C&C3) depend on the XP behavior, mark the Win7 one broken */
311         ok(SUCCEEDED(hr) || broken(hr == test_data[i].win7_result),
312                 "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
313                 hr, rect->left, rect->top, rect->right, rect->bottom);
314         if(FAILED(hr)) continue;
315
316         offset = (BYTE *)locked_rect.pBits - base;
317         expected_offset = rect->top * locked_rect.Pitch + rect->left * 4;
318         ok(offset == expected_offset, "Got offset %u, expected offset %u for rect [%d, %d]->[%d, %d]\n",
319                 offset, expected_offset, rect->left, rect->top, rect->right, rect->bottom);
320
321         hr = IDirect3DSurface9_UnlockRect(surface);
322         ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
323     }
324
325     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
326     ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect NULL\n", hr);
327     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
328     ok(hr == D3DERR_INVALIDCALL, "Double LockRect for rect NULL returned 0x%08x\n", hr);
329     hr = IDirect3DSurface9_UnlockRect(surface);
330     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
331
332     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0].rect, 0);
333     ok(hr == D3D_OK, "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
334             ", expected D3D_OK (0x%08x)\n", hr, test_data[0].rect.left, test_data[0].rect.top,
335             test_data[0].rect.right, test_data[0].rect.bottom, D3D_OK);
336     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0].rect, 0);
337     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
338             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_data[0].rect.left, test_data[0].rect.top,
339             test_data[0].rect.right, test_data[0].rect.bottom, D3DERR_INVALIDCALL);
340     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_rect_2, 0);
341     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
342             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_rect_2.left, test_rect_2.top,
343             test_rect_2.right, test_rect_2.bottom, D3DERR_INVALIDCALL);
344     hr = IDirect3DSurface9_UnlockRect(surface);
345     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
346
347     IDirect3DSurface9_Release(surface);
348 }
349
350 static ULONG getref(IUnknown *iface)
351 {
352     IUnknown_AddRef(iface);
353     return IUnknown_Release(iface);
354 }
355
356 static void test_private_data(IDirect3DDevice9 *device)
357 {
358     HRESULT hr;
359     IDirect3DSurface9 *surface;
360     ULONG ref, ref2;
361     IUnknown *ptr;
362     DWORD size = sizeof(IUnknown *);
363
364     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
365     ok(SUCCEEDED(hr), "CreateImageSurface failed (0x%08x)\n", hr);
366     if(!surface)
367     {
368         return;
369     }
370
371     /* This fails */
372     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 0, D3DSPD_IUNKNOWN);
373     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
374     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 5, D3DSPD_IUNKNOWN);
375     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
376     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *) * 2, D3DSPD_IUNKNOWN);
377     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
378
379     ref = getref((IUnknown *) device);
380     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
381     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
382     ref2 = getref((IUnknown *) device);
383     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
384     hr = IDirect3DSurface9_FreePrivateData(surface, &IID_IDirect3DSurface9);
385     ok(hr == D3D_OK, "IDirect3DSurface9_FreePrivateData returned %08x\n", hr);
386     ref2 = getref((IUnknown *) device);
387     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
388
389     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
390     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
391     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, surface, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
392     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
393     ref2 = getref((IUnknown *) device);
394     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
395
396     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
397     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
398     hr = IDirect3DSurface9_GetPrivateData(surface, &IID_IDirect3DSurface9, &ptr, &size);
399     ok(hr == D3D_OK, "IDirect3DSurface9_GetPrivateData failed with %08x\n", hr);
400     ref2 = getref((IUnknown *) device);
401     /* Object is addrefed */
402     ok(ptr == (IUnknown *) device, "Returned interface pointer is %p, expected %p\n", ptr, device);
403     ok(ref2 == ref + 2, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 2, ptr, device);
404     IUnknown_Release(ptr);
405
406     IDirect3DSurface9_Release(surface);
407
408     /* Destroying the surface frees the held reference */
409     ref2 = getref((IUnknown *) device);
410     /* -1 because the surface was released and held a reference before */
411     ok(ref2 == (ref - 1), "Object reference is %d, expected %d\n", ref2, (ref - 1));
412 }
413
414 static void test_getdc(IDirect3DDevice9 *device)
415 {
416     IDirect3DSurface9 *surface;
417     IDirect3DTexture9 *texture;
418     HRESULT hr;
419     unsigned int i;
420     HDC dc;
421
422     struct
423     {
424         const char *name;
425         D3DFORMAT fmt;
426         BOOL getdc_supported;
427     } testdata[] = {
428         { "D3DFMT_A8R8G8B8",    D3DFMT_A8R8G8B8,    TRUE    },
429         { "D3DFMT_X8R8G8B8",    D3DFMT_X8R8G8B8,    TRUE    },
430         { "D3DFMT_R5G6B5",      D3DFMT_R5G6B5,      TRUE    },
431         { "D3DFMT_X1R5G5B5",    D3DFMT_X1R5G5B5,    TRUE    },
432         { "D3DFMT_A1R5G5B5",    D3DFMT_A1R5G5B5,    TRUE    },
433         { "D3DFMT_R8G8B8",      D3DFMT_R8G8B8,      TRUE    },
434         { "D3DFMT_A2R10G10B10", D3DFMT_A2R10G10B10, FALSE   }, /* Untested, card on windows didn't support it */
435         { "D3DFMT_V8U8",        D3DFMT_V8U8,        FALSE   },
436         { "D3DFMT_Q8W8V8U8",    D3DFMT_Q8W8V8U8,    FALSE   },
437         { "D3DFMT_A8B8G8R8",    D3DFMT_A8B8G8R8,    FALSE   },
438         { "D3DFMT_X8B8G8R8",    D3DFMT_A8B8G8R8,    FALSE   },
439         { "D3DFMT_R3G3B2",      D3DFMT_R3G3B2,      FALSE   },
440         { "D3DFMT_P8",          D3DFMT_P8,          FALSE   },
441         { "D3DFMT_L8",          D3DFMT_L8,          FALSE   },
442         { "D3DFMT_A8L8",        D3DFMT_A8L8,        FALSE   },
443         { "D3DFMT_DXT1",        D3DFMT_DXT1,        FALSE   },
444         { "D3DFMT_DXT2",        D3DFMT_DXT2,        FALSE   },
445         { "D3DFMT_DXT3",        D3DFMT_DXT3,        FALSE   },
446         { "D3DFMT_DXT4",        D3DFMT_DXT4,        FALSE   },
447         { "D3DFMT_DXT5",        D3DFMT_DXT5,        FALSE   },
448     };
449
450     for(i = 0; i < (sizeof(testdata) / sizeof(testdata[0])); i++)
451     {
452         texture = NULL;
453         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 64, 64, testdata[i].fmt, D3DPOOL_SYSTEMMEM, &surface, NULL);
454         if(FAILED(hr))
455         {
456             hr = IDirect3DDevice9_CreateTexture(device, 64, 64, 1, 0, testdata[i].fmt, D3DPOOL_MANAGED, &texture, NULL);
457             if(FAILED(hr))
458             {
459                 skip("IDirect3DDevice9_CreateOffscreenPlainSurface failed, hr = 0x%08x, fmt %s\n", hr, testdata[i].name);
460                 continue;
461             }
462             IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
463         }
464
465         dc = (void *) 0x1234;
466         hr = IDirect3DSurface9_GetDC(surface, &dc);
467
468         if(testdata[i].getdc_supported)
469         {
470             ok(SUCCEEDED(hr), "GetDC on format %s failed(hr=0x%08x), but was expected to work\n",
471                testdata[i].name, hr);
472         }
473         else
474         {
475             ok(FAILED(hr), "GetDC on format %s worked(hr=0x%08x), but was expected to fail\n",
476                testdata[i].name, hr);
477         }
478
479         if(SUCCEEDED(hr))
480         {
481             hr = IDirect3DSurface9_ReleaseDC(surface, dc);
482             ok(hr == D3D_OK, "IDirect3DSurface9_ReleaseDC failed, hr = 0x%08x\n", hr);
483         }
484         else
485         {
486             ok(dc == (void *) 0x1234, "After failed getDC dc is %p\n", dc);
487         }
488
489         IDirect3DSurface9_Release(surface);
490         if(texture) IDirect3DTexture9_Release(texture);
491     }
492 }
493
494 START_TEST(surface)
495 {
496     HMODULE d3d9_handle;
497     IDirect3DDevice9 *device_ptr;
498     ULONG refcount;
499
500     d3d9_handle = LoadLibraryA("d3d9.dll");
501     if (!d3d9_handle)
502     {
503         skip("Could not load d3d9.dll\n");
504         return;
505     }
506
507     device_ptr = init_d3d9(d3d9_handle);
508     if (!device_ptr) return;
509
510     test_surface_get_container(device_ptr);
511     test_surface_alignment(device_ptr);
512     test_lockrect_offset(device_ptr);
513     test_lockrect_invalid(device_ptr);
514     test_private_data(device_ptr);
515     test_getdc(device_ptr);
516
517     refcount = IDirect3DDevice9_Release(device_ptr);
518     ok(!refcount, "Device has %u references left\n", refcount);
519 }