[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / 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, out %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
109 TRACE("iface %p.\n", iface);
110
111 return query->data_size;
112 }
113
114 static HRESULT WINAPI d3d9_query_Issue(IDirect3DQuery9 *iface, DWORD flags)
115 {
116 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
117 HRESULT hr;
118
119 TRACE("iface %p, flags %#x.\n", iface, flags);
120
121 wined3d_mutex_lock();
122 hr = wined3d_query_issue(query->wined3d_query, flags);
123 wined3d_mutex_unlock();
124
125 return hr;
126 }
127
128 static HRESULT WINAPI d3d9_query_GetData(IDirect3DQuery9 *iface, void *data, DWORD size, DWORD flags)
129 {
130 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
131 enum wined3d_query_type type;
132 HRESULT hr;
133
134 TRACE("iface %p, data %p, size %u, flags %#x.\n",
135 iface, data, size, flags);
136
137 wined3d_mutex_lock();
138 type = wined3d_query_get_type(query->wined3d_query);
139 if (type == WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT && data)
140 {
141 struct wined3d_query_data_timestamp_disjoint data_disjoint;
142
143 if (size > sizeof(data_disjoint.disjoint))
144 size = sizeof(data_disjoint.disjoint);
145
146 hr = wined3d_query_get_data(query->wined3d_query, &data_disjoint, sizeof(data_disjoint), flags);
147 if (SUCCEEDED(hr))
148 memcpy(data, &data_disjoint.disjoint, size);
149 }
150 else
151 {
152 hr = wined3d_query_get_data(query->wined3d_query, data, size, flags);
153 }
154 wined3d_mutex_unlock();
155
156 if (hr == D3DERR_INVALIDCALL)
157 {
158 if (data)
159 {
160 memset(data, 0, size);
161 memset(data, 0xdd, min(size, query->data_size));
162 }
163 return S_OK;
164 }
165 return hr;
166 }
167
168
169 static const struct IDirect3DQuery9Vtbl d3d9_query_vtbl =
170 {
171 d3d9_query_QueryInterface,
172 d3d9_query_AddRef,
173 d3d9_query_Release,
174 d3d9_query_GetDevice,
175 d3d9_query_GetType,
176 d3d9_query_GetDataSize,
177 d3d9_query_Issue,
178 d3d9_query_GetData,
179 };
180
181 HRESULT query_init(struct d3d9_query *query, struct d3d9_device *device, D3DQUERYTYPE type)
182 {
183 HRESULT hr;
184
185 if (type > D3DQUERYTYPE_MEMORYPRESSURE)
186 {
187 WARN("Invalid query type %#x.\n", type);
188 return D3DERR_NOTAVAILABLE;
189 }
190
191 query->IDirect3DQuery9_iface.lpVtbl = &d3d9_query_vtbl;
192 query->refcount = 1;
193
194 wined3d_mutex_lock();
195 if (FAILED(hr = wined3d_query_create(device->wined3d_device, type,
196 query, &d3d9_null_wined3d_parent_ops, &query->wined3d_query)))
197 {
198 wined3d_mutex_unlock();
199 WARN("Failed to create wined3d query, hr %#x.\n", hr);
200 return hr;
201 }
202
203 if (type == D3DQUERYTYPE_OCCLUSION)
204 query->data_size = sizeof(DWORD);
205 else if (type == D3DQUERYTYPE_TIMESTAMPDISJOINT)
206 query->data_size = sizeof(BOOL);
207 else
208 query->data_size = wined3d_query_get_data_size(query->wined3d_query);
209 wined3d_mutex_unlock();
210
211 query->parent_device = &device->IDirect3DDevice9Ex_iface;
212 IDirect3DDevice9Ex_AddRef(query->parent_device);
213
214 return D3D_OK;
215 }