wined3d: Implement more GLSL instructions and a little cleanup.
[wine] / dlls / ddraw / tests / refcount.c
1 /*
2  * Some unit tests for ddraw reference counting
3  *
4  * Copyright (C) 2006 Stefan Dösinger
5  *
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.
10  *
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.
15  *
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
19  */
20 #define COBJMACROS
21
22 #include <assert.h>
23 #include "wine/test.h"
24 #include "ddraw.h"
25 #include "d3d.h"
26 #include "unknwn.h"
27
28 static HRESULT (WINAPI *pDirectDrawCreateEx)(LPGUID,LPVOID*,REFIID,LPUNKNOWN);
29
30 static void init_function_pointers(void)
31 {
32     HMODULE hmod = GetModuleHandleA("ddraw.dll");
33
34     if(hmod)
35     {
36         pDirectDrawCreateEx = (void*)GetProcAddress(hmod, "DirectDrawCreateEx");
37     }
38 }
39
40 unsigned long getRefcount(IUnknown *iface)
41 {
42     IUnknown_AddRef(iface);
43     return IUnknown_Release(iface);
44 }
45
46 static void test_ddraw(void)
47 {
48     HRESULT hr;
49     unsigned long ref;
50     IDirectDraw7 *DDraw;
51     IDirectDrawPalette *palette;
52     IDirectDrawSurface7 *surface;
53     PALETTEENTRY Table[256];
54     DDSURFACEDESC2 ddsd;
55
56     hr = pDirectDrawCreateEx(NULL, (void **) &DDraw, &IID_IDirectDraw7, NULL);
57     ok(hr == DD_OK || hr==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %lx\n", hr);
58     if(!DDraw)
59     {
60         trace("Couldn't create DDraw interface, skipping tests\n");
61         return;
62     }
63
64     ref = getRefcount( (IUnknown *) DDraw);
65     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
66
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);
70
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);
74
75     hr = IDirectDraw7_SetCooperativeLevel(DDraw, 0, DDSCL_NORMAL);
76     ok(hr == DD_OK, "SetCooperativeLevel failed with %08lx\n", hr);
77
78     memset(&ddsd, 0, sizeof(ddsd));
79     ddsd.dwSize = sizeof(ddsd);
80     ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
81     ddsd.dwWidth = 64;
82     ddsd.dwHeight = 64;
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;
87
88     hr = IDirectDraw7_CreateSurface(DDraw, &ddsd, &surface, NULL);
89     ok(hr == DD_OK, "CreateSurface failed with %08lx\n", hr);
90
91     /* DDraw refcount increased by 1 */
92     ref = getRefcount( (IUnknown *) DDraw);
93     ok(ref == 2, "Got refcount %ld, expected 2\n", ref);
94
95     /* Surface refcount starts with 1 */
96     ref = getRefcount( (IUnknown *) surface);
97     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
98
99     hr = IDirectDraw7_CreatePalette(DDraw, DDPCAPS_ALLOW256 | DDPCAPS_8BIT, Table, &palette, NULL);
100     ok(hr == DD_OK, "CreatePalette returned %08lx\n", hr);
101
102     /* DDraw refcount increased by 1 */
103     ref = getRefcount( (IUnknown *) DDraw);
104     ok(ref == 3, "Got refcount %ld, expected 3\n", ref);
105
106     /* Palette starts with 1 */
107     ref = getRefcount( (IUnknown *) palette);
108     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
109
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);
113
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);
119
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);
124
125     /* Releasing the surface detaches the palette */
126     ref = getRefcount( (IUnknown *) palette);
127     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
128
129     IDirectDrawPalette_Release(palette);
130
131     /* Incresed before - decrease now */
132     ref = getRefcount( (IUnknown *) DDraw);
133     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
134
135     IDirectDraw7_Release(DDraw);
136 }
137
138 START_TEST(refcount)
139 {
140     init_function_pointers();
141     if(!pDirectDrawCreateEx)
142     {
143         trace("function DirectDrawCreateEx not available, skipping tests\n");
144         return;
145     }
146     test_ddraw();
147 }