044a2ad4f9bd2cb615c60143c059a358e68ad951
[reactos.git] / reactos / dll / directx / wine / d3d9 / query.c
1 /*
2 * IDirect3DQuery9 implementation
3 *
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "d3d9_private.h"
24
25 static inline struct d3d9_query *impl_from_IDirect3DQuery9(IDirect3DQuery9 *iface)
26 {
27 return CONTAINING_RECORD(iface, struct d3d9_query, IDirect3DQuery9_iface);
28 }
29
30 static HRESULT WINAPI d3d9_query_QueryInterface(IDirect3DQuery9 *iface, REFIID riid, void **out)
31 {
32 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), out);
33
34 if (IsEqualGUID(riid, &IID_IDirect3DQuery9)
35 || IsEqualGUID(riid, &IID_IUnknown))
36 {
37 IDirect3DQuery9_AddRef(iface);
38 *out = iface;
39 return S_OK;
40 }
41
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
43
44 *out = NULL;
45 return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI d3d9_query_AddRef(IDirect3DQuery9 *iface)
49 {
50 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
51 ULONG refcount = InterlockedIncrement(&query->refcount);
52
53 TRACE("%p increasing refcount to %u.\n", iface, refcount);
54
55 return refcount;
56 }
57
58 static ULONG WINAPI d3d9_query_Release(IDirect3DQuery9 *iface)
59 {
60 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
61 ULONG refcount = InterlockedDecrement(&query->refcount);
62
63 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
64
65 if (!refcount)
66 {
67 wined3d_mutex_lock();
68 wined3d_query_decref(query->wined3d_query);
69 wined3d_mutex_unlock();
70
71 IDirect3DDevice9Ex_Release(query->parent_device);
72 HeapFree(GetProcessHeap(), 0, query);
73 }
74 return refcount;
75 }
76
77 static HRESULT WINAPI d3d9_query_GetDevice(IDirect3DQuery9 *iface, IDirect3DDevice9 **device)
78 {
79 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
80
81 TRACE("iface %p, device %p.\n", iface, device);
82
83 *device = (IDirect3DDevice9 *)query->parent_device;
84 IDirect3DDevice9_AddRef(*device);
85
86 TRACE("Returning device %p.\n", *device);
87
88 return D3D_OK;
89 }
90
91 static D3DQUERYTYPE WINAPI d3d9_query_GetType(IDirect3DQuery9 *iface)
92 {
93 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
94 D3DQUERYTYPE type;
95
96 TRACE("iface %p.\n", iface);
97
98 wined3d_mutex_lock();
99 type = wined3d_query_get_type(query->wined3d_query);
100 wined3d_mutex_unlock();
101
102 return type;
103 }
104
105 static DWORD WINAPI d3d9_query_GetDataSize(IDirect3DQuery9 *iface)
106 {
107 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
108 enum wined3d_query_type type;
109 DWORD ret;
110
111 TRACE("iface %p.\n", iface);
112
113 wined3d_mutex_lock();
114 type = wined3d_query_get_type(query->wined3d_query);
115 if (type == WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT)
116 ret = sizeof(BOOL);
117 else
118 ret = wined3d_query_get_data_size(query->wined3d_query);
119 wined3d_mutex_unlock();
120
121 return ret;
122 }
123
124 static HRESULT WINAPI d3d9_query_Issue(IDirect3DQuery9 *iface, DWORD flags)
125 {
126 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
127 HRESULT hr;
128
129 TRACE("iface %p, flags %#x.\n", iface, flags);
130
131 wined3d_mutex_lock();
132 hr = wined3d_query_issue(query->wined3d_query, flags);
133 wined3d_mutex_unlock();
134
135 return hr;
136 }
137
138 static HRESULT WINAPI d3d9_query_GetData(IDirect3DQuery9 *iface, void *data, DWORD size, DWORD flags)
139 {
140 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
141 enum wined3d_query_type type;
142 HRESULT hr;
143
144 TRACE("iface %p, data %p, size %u, flags %#x.\n",
145 iface, data, size, flags);
146
147 wined3d_mutex_lock();
148 type = wined3d_query_get_type(query->wined3d_query);
149 if (type == WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT && data)
150 {
151 struct wined3d_query_data_timestamp_disjoint data_disjoint;
152
153 hr = wined3d_query_get_data(query->wined3d_query, &data_disjoint, sizeof(data_disjoint), flags);
154 *(BOOL *)data = data_disjoint.disjoint;
155 }
156 else
157 {
158 hr = wined3d_query_get_data(query->wined3d_query, data, size, flags);
159 }
160 wined3d_mutex_unlock();
161
162 return hr;
163 }
164
165
166 static const struct IDirect3DQuery9Vtbl d3d9_query_vtbl =
167 {
168 d3d9_query_QueryInterface,
169 d3d9_query_AddRef,
170 d3d9_query_Release,
171 d3d9_query_GetDevice,
172 d3d9_query_GetType,
173 d3d9_query_GetDataSize,
174 d3d9_query_Issue,
175 d3d9_query_GetData,
176 };
177
178 HRESULT query_init(struct d3d9_query *query, struct d3d9_device *device, D3DQUERYTYPE type)
179 {
180 HRESULT hr;
181
182 query->IDirect3DQuery9_iface.lpVtbl = &d3d9_query_vtbl;
183 query->refcount = 1;
184
185 wined3d_mutex_lock();
186 hr = wined3d_query_create(device->wined3d_device, type, &query->wined3d_query);
187 wined3d_mutex_unlock();
188 if (FAILED(hr))
189 {
190 WARN("Failed to create wined3d query, hr %#x.\n", hr);
191 return hr;
192 }
193
194 query->parent_device = &device->IDirect3DDevice9Ex_iface;
195 IDirect3DDevice9Ex_AddRef(query->parent_device);
196
197 return D3D_OK;
198 }