mshtml: Added IHTMLElement2::get_currentStyle implementation.
[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     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
47     if (!d3d9_ptr) return NULL;
48
49     ZeroMemory(&present_parameters, sizeof(present_parameters));
50     present_parameters.Windowed = TRUE;
51     present_parameters.hDeviceWindow = create_window();
52     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
53
54     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
55             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
56
57     if(FAILED(hr))
58     {
59         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
60         return NULL;
61     }
62
63     return device_ptr;
64 }
65
66 static void test_surface_get_container(IDirect3DDevice9 *device_ptr)
67 {
68     IDirect3DTexture9 *texture_ptr = 0;
69     IDirect3DSurface9 *surface_ptr = 0;
70     void *container_ptr;
71     HRESULT hr;
72
73     hr = IDirect3DDevice9_CreateTexture(device_ptr, 128, 128, 1, 0,
74             D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture_ptr, 0);
75     ok(SUCCEEDED(hr) && texture_ptr != NULL, "CreateTexture returned: hr %#x, texture_ptr %p. "
76         "Expected hr %#x, texture_ptr != %p\n", hr, texture_ptr, D3D_OK, NULL);
77     if (!texture_ptr || FAILED(hr)) goto cleanup;
78
79     hr = IDirect3DTexture9_GetSurfaceLevel(texture_ptr, 0, &surface_ptr);
80     ok(SUCCEEDED(hr) && surface_ptr != NULL, "GetSurfaceLevel returned: hr %#x, surface_ptr %p. "
81         "Expected hr %#x, surface_ptr != %p\n", hr, surface_ptr, D3D_OK, NULL);
82     if (!surface_ptr || FAILED(hr)) goto cleanup;
83
84     /* These should work... */
85     container_ptr = (void *)0x1337c0d3;
86     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IUnknown, &container_ptr);
87     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
88         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
89     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
90
91     container_ptr = (void *)0x1337c0d3;
92     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DResource9, &container_ptr);
93     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
94         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
95     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
96
97     container_ptr = (void *)0x1337c0d3;
98     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DBaseTexture9, &container_ptr);
99     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
100         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
101     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
102
103     container_ptr = (void *)0x1337c0d3;
104     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DTexture9, &container_ptr);
105     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
106         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
107     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
108
109     /* ...and this one shouldn't. This should return E_NOINTERFACE and set container_ptr to NULL */
110     container_ptr = (void *)0x1337c0d3;
111     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DSurface9, &container_ptr);
112     ok(hr == E_NOINTERFACE && container_ptr == NULL, "GetContainer returned: hr %#x, container_ptr %p. "
113         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, E_NOINTERFACE, NULL);
114     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
115
116 cleanup:
117     if (texture_ptr) IDirect3DTexture9_Release(texture_ptr);
118     if (surface_ptr) IDirect3DSurface9_Release(surface_ptr);
119 }
120
121 static void test_surface_alignment(IDirect3DDevice9 *device_ptr)
122 {
123     IDirect3DSurface9 *surface_ptr = 0;
124     HRESULT hr;
125     int i;
126
127     /* Test a sysmem surface as those aren't affected by the hardware's np2 restrictions */
128     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device_ptr, 5, 5, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface_ptr, 0);
129     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
130
131     if(surface_ptr)
132     {
133         D3DLOCKED_RECT lockedRect;
134         hr = IDirect3DSurface9_LockRect(surface_ptr, &lockedRect, NULL, 0);
135         ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
136         ok(!(lockedRect.Pitch & 3), "Surface pitch %d is not 32-bit aligned\n", lockedRect.Pitch);
137         /* Some applications also depend on the exact pitch, rather than just
138          * the alignment.
139          */
140         ok(lockedRect.Pitch == 12, "Got pitch %d, expected 12\n", lockedRect.Pitch);
141         hr = IDirect3DSurface9_UnlockRect(surface_ptr);
142         IDirect3DSurface9_Release(surface_ptr);
143     }
144
145     for (i = 0; i < 5; i++)
146     {
147         IDirect3DTexture9 *pTexture;
148         int j, pitch;
149
150         hr = IDirect3DDevice9_CreateTexture(device_ptr, 64, 64, 0, 0, MAKEFOURCC('D', 'X', 'T', '1'+i),
151                                             D3DPOOL_MANAGED, &pTexture, NULL);
152         ok(SUCCEEDED(hr) || hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_CreateTexture: %08x\n", hr);
153         if (FAILED(hr)) {
154             skip("DXT%d surfaces are not supported\n", i + 1);
155             continue;
156         }
157
158         for (j = IDirect3DBaseTexture9_GetLevelCount(pTexture) - 1; j >= 0; j--)
159         {
160             D3DLOCKED_RECT rc;
161             D3DSURFACE_DESC descr;
162
163             IDirect3DTexture9_GetLevelDesc(pTexture, j, &descr);
164             hr = IDirect3DTexture9_LockRect(pTexture, j, &rc, NULL, 0);
165             ok(SUCCEEDED(hr), "IDirect3DTexture9_LockRect: %08x\n", hr);
166             IDirect3DTexture9_UnlockRect(pTexture, j);
167
168             pitch = ((descr.Width + 3) >> 2) << 3;
169             if (i > 0) pitch <<= 1;
170             ok(rc.Pitch == pitch, "Wrong pitch for DXT%d lvl[%d (%dx%d)]: expected %d got %d\n",
171                i + 1, j, descr.Width, descr.Height, pitch, rc.Pitch);
172         }
173         IUnknown_Release( pTexture );
174     }
175 }
176
177 /* Since the DXT formats are based on 4x4 blocks, locking works slightly
178  * different than with regular formats. This patch verifies we return the
179  * correct memory offsets */
180 static void test_lockrect_offset(IDirect3DDevice9 *device)
181 {
182     IDirect3DSurface9 *surface = 0;
183     const RECT rect = {60, 60, 68, 68};
184     D3DLOCKED_RECT locked_rect;
185     unsigned int expected_offset;
186     unsigned int offset;
187     unsigned int i;
188     BYTE *base;
189     HRESULT hr;
190
191     const struct {
192         D3DFORMAT fmt;
193         const char *name;
194         unsigned int block_width;
195         unsigned int block_height;
196         unsigned int block_size;
197     } dxt_formats[] = {
198         {D3DFMT_DXT1, "D3DFMT_DXT1", 4, 4, 8},
199         {D3DFMT_DXT2, "D3DFMT_DXT2", 4, 4, 16},
200         {D3DFMT_DXT3, "D3DFMT_DXT3", 4, 4, 16},
201         {D3DFMT_DXT4, "D3DFMT_DXT4", 4, 4, 16},
202         {D3DFMT_DXT5, "D3DFMT_DXT5", 4, 4, 16},
203     };
204
205     for (i = 0; i < (sizeof(dxt_formats) / sizeof(*dxt_formats)); ++i) {
206         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, dxt_formats[i].fmt, D3DPOOL_SCRATCH, &surface, 0);
207         ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (%08x)\n", hr);
208
209         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
210         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
211
212         base = locked_rect.pBits;
213
214         hr = IDirect3DSurface9_UnlockRect(surface);
215         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
216
217         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &rect, 0);
218         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
219
220         offset = (BYTE *)locked_rect.pBits - base;
221         expected_offset = (rect.top / dxt_formats[i].block_height) * locked_rect.Pitch
222                         + (rect.left / dxt_formats[i].block_width) * dxt_formats[i].block_size;
223         ok(offset == expected_offset, "Got offset %u, expected offset %u for format %s\n", offset, expected_offset, dxt_formats[i].name);
224
225         hr = IDirect3DSurface9_UnlockRect(surface);
226         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
227
228         IDirect3DSurface9_Release(surface);
229     }
230 }
231
232 static void test_lockrect_invalid(IDirect3DDevice9 *device)
233 {
234     IDirect3DSurface9 *surface = 0;
235     D3DLOCKED_RECT locked_rect;
236     unsigned int i;
237     BYTE *base;
238     HRESULT hr;
239
240     const RECT test_rect_2 = { 0, 0, 8, 8 };
241     const RECT test_data[] = {
242         {60, 60, 68, 68},       /* Valid */
243         {60, 60, 60, 68},       /* 0 height */
244         {60, 60, 68, 60},       /* 0 width */
245         {68, 60, 60, 68},       /* left > right */
246         {60, 68, 68, 60},       /* top > bottom */
247         {-8, 60,  0, 68},       /* left < surface */
248         {60, -8, 68,  0},       /* top < surface */
249         {-16, 60, -8, 68},      /* right < surface */
250         {60, -16, 68, -8},      /* bottom < surface */
251         {60, 60, 136, 68},      /* right > surface */
252         {60, 60, 68, 136},      /* bottom > surface */
253         {136, 60, 144, 68},     /* left > surface */
254         {60, 136, 68, 144},     /* top > surface */
255     };
256
257     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
258     ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (0x%08x)\n", hr);
259
260     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
261     ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
262
263     base = locked_rect.pBits;
264
265     hr = IDirect3DSurface9_UnlockRect(surface);
266     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
267
268     for (i = 0; i < (sizeof(test_data) / sizeof(*test_data)); ++i)
269     {
270         unsigned int offset, expected_offset;
271         const RECT *rect = &test_data[i];
272
273         locked_rect.pBits = (BYTE *)0xdeadbeef;
274         locked_rect.Pitch = 0xdeadbeef;
275
276         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, rect, 0);
277         ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
278                 hr, rect->left, rect->top, rect->right, rect->bottom);
279
280         offset = (BYTE *)locked_rect.pBits - base;
281         expected_offset = rect->top * locked_rect.Pitch + rect->left * 4;
282         ok(offset == expected_offset, "Got offset %u, expected offset %u for rect [%d, %d]->[%d, %d]\n",
283                 offset, expected_offset, rect->left, rect->top, rect->right, rect->bottom);
284
285         hr = IDirect3DSurface9_UnlockRect(surface);
286         ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
287     }
288
289     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
290     ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect NULL\n", hr);
291     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
292     ok(hr == D3DERR_INVALIDCALL, "Double LockRect for rect NULL returned 0x%08x\n", hr);
293     hr = IDirect3DSurface9_UnlockRect(surface);
294     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
295
296     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0], 0);
297     ok(hr == D3D_OK, "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
298             ", expected D3D_OK (0x%08x)\n", hr, test_data[0].left, test_data[0].top,
299             test_data[0].right, test_data[0].bottom, D3D_OK);
300     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0], 0);
301     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
302             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_data[0].left, test_data[0].top,
303             test_data[0].right, test_data[0].bottom, D3DERR_INVALIDCALL);
304     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_rect_2, 0);
305     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
306             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_rect_2.left, test_rect_2.top,
307             test_rect_2.right, test_rect_2.bottom, D3DERR_INVALIDCALL);
308     hr = IDirect3DSurface9_UnlockRect(surface);
309     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
310
311     IDirect3DSurface9_Release(surface);
312 }
313
314 static unsigned long getref(IUnknown *iface)
315 {
316     IUnknown_AddRef(iface);
317     return IUnknown_Release(iface);
318 }
319
320 static void test_private_data(IDirect3DDevice9 *device)
321 {
322     HRESULT hr;
323     IDirect3DSurface9 *surface;
324     ULONG ref, ref2;
325     IUnknown *ptr;
326     DWORD size = sizeof(IUnknown *);
327
328     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
329     ok(SUCCEEDED(hr), "CreateImageSurface failed (0x%08x)\n", hr);
330     if(!surface)
331     {
332         return;
333     }
334
335     /* This fails */
336     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 0, D3DSPD_IUNKNOWN);
337     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
338     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 5, D3DSPD_IUNKNOWN);
339     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
340     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *) * 2, D3DSPD_IUNKNOWN);
341     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
342
343     ref = getref((IUnknown *) device);
344     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
345     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
346     ref2 = getref((IUnknown *) device);
347     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
348     hr = IDirect3DSurface9_FreePrivateData(surface, &IID_IDirect3DSurface9);
349     ok(hr == D3D_OK, "IDirect3DSurface9_FreePrivateData returned %08x\n", hr);
350     ref2 = getref((IUnknown *) device);
351     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
352
353     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
354     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
355     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, surface, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
356     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
357     ref2 = getref((IUnknown *) device);
358     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
359
360     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
361     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
362     hr = IDirect3DSurface9_GetPrivateData(surface, &IID_IDirect3DSurface9, &ptr, &size);
363     ok(hr == D3D_OK, "IDirect3DSurface9_GetPrivateData failed with %08x\n", hr);
364     ref2 = getref((IUnknown *) device);
365     /* Object is NOT being addrefed */
366     ok(ptr == (IUnknown *) device, "Returned interface pointer is %p, expected %p\n", ptr, device);
367     ok(ref2 == ref + 2, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 2, ptr, device);
368     IUnknown_Release(ptr);
369
370     IDirect3DSurface9_Release(surface);
371
372     /* Destroying the surface frees the held reference */
373     ref2 = getref((IUnknown *) device);
374     /* -1 because the surface was released and held a reference before */
375     ok(ref2 == (ref - 1), "Object reference is %d, expected %d\n", ref2, (ref - 1));
376 }
377
378 START_TEST(surface)
379 {
380     HMODULE d3d9_handle;
381     IDirect3DDevice9 *device_ptr;
382
383     d3d9_handle = LoadLibraryA("d3d9.dll");
384     if (!d3d9_handle)
385     {
386         skip("Could not load d3d9.dll\n");
387         return;
388     }
389
390     device_ptr = init_d3d9(d3d9_handle);
391     if (!device_ptr) return;
392
393     test_surface_get_container(device_ptr);
394     test_surface_alignment(device_ptr);
395     test_lockrect_offset(device_ptr);
396     test_lockrect_invalid(device_ptr);
397     test_private_data(device_ptr);
398 }