2 * Some unit tests for ddraw reference counting
4 * Copyright (C) 2006 Stefan Dösinger
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/test.h"
28 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
30 static void init_function_pointers(void)
32 HMODULE hmod = GetModuleHandleA("ddraw.dll");
36 pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
40 unsigned long getRefcount(IUnknown *iface)
42 IUnknown_AddRef(iface);
43 return IUnknown_Release(iface);
46 static void test_ddraw(void)
51 IDirectDrawPalette *palette;
52 IDirectDrawSurface7 *surface;
53 PALETTEENTRY Table[256];
56 hr = pDirectDrawCreateEx(NULL, (void **) &DDraw, &IID_IDirectDraw7, NULL);
57 ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %lx\n", hr);
60 trace("Couldn't create DDraw interface, skipping tests\n");
64 ref = getRefcount( (IUnknown *) DDraw);
65 ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
67 /* Fails without a cooplevel */
68 hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
69 ok(hr == DDERR_NOCOOPERATIVELEVELSET, "CreatePalette returned %08lx\n", hr);
71 /* This check is before the cooplevel check */
72 hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, (void *) 0xdeadbeef);
73 ok(hr == CLASS_E_NOAGGREGATION, "CreatePalette returned %08lx\n", hr);
75 hr = IDirectDraw7_SetCooperativeLevel(DDraw, 0, DDSCL_NORMAL);
76 ok(hr == DD_OK, "SetCooperativeLevel failed with %08lx\n", hr);
78 memset(&ddsd, 0, sizeof(ddsd));
79 ddsd.dwSize = sizeof(ddsd);
80 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
83 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
84 U4(ddsd).ddpfPixelFormat.dwSize = sizeof(U4(ddsd).ddpfPixelFormat);
85 U4(ddsd).ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8 | DDPF_RGB;
86 U1(U4(ddsd).ddpfPixelFormat).dwRGBBitCount = 8;
88 hr = IDirectDraw7_CreateSurface(DDraw, &ddsd, &surface, NULL);
89 ok(hr == DD_OK, "CreateSurface failed with %08lx\n", hr);
91 /* DDraw refcount increased by 1 */
92 ref = getRefcount( (IUnknown *) DDraw);
93 ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
95 /* Surface refcount starts with 1 */
96 ref = getRefcount( (IUnknown *) surface);
97 ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
99 hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
100 ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr);
102 /* DDraw refcount increased by 1 */
103 ref = getRefcount( (IUnknown *) DDraw);
104 ok(ref == 3, "Got refcount %ld, expected 3\n", ref);
106 /* Palette starts with 1 */
107 ref = getRefcount( (IUnknown *) palette);
108 ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
110 /* Test attaching a palette to a surface */
111 hr = IDirectDrawSurface7_SetPalette(surface, palette);
112 ok(hr == DD_OK, "IDirectDrawSurface_SetPalette failed with %08lx\n", hr);
114 /* Palette refcount increased, surface stays the same */
115 ref = getRefcount( (IUnknown *) palette);
116 ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
117 ref = getRefcount( (IUnknown *) surface);
118 ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
120 IDirectDrawSurface7_Release(surface);
121 /* Incresed before - decrease now */
122 ref = getRefcount( (IUnknown *) DDraw);
123 ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
125 /* Releasing the surface detaches the palette */
126 ref = getRefcount( (IUnknown *) palette);
127 ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
129 IDirectDrawPalette_Release(palette);
131 /* Incresed before - decrease now */
132 ref = getRefcount( (IUnknown *) DDraw);
133 ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
135 IDirectDraw7_Release(DDraw);
140 init_function_pointers();
141 if(!pDirectDrawCreateEx)
143 trace("function DirectDrawCreateEx not available, skipping tests\n");