d3d9: Build with msvcrt.
[wine.git] / dlls / d3d9 / query.c
blobe9180fb481fd318ab3183156162218d292ae4a1e
1 /*
2 * IDirect3DQuery9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
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.
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.
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
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static inline struct d3d9_query *impl_from_IDirect3DQuery9(IDirect3DQuery9 *iface)
29 return CONTAINING_RECORD(iface, struct d3d9_query, IDirect3DQuery9_iface);
32 static HRESULT WINAPI d3d9_query_QueryInterface(IDirect3DQuery9 *iface, REFIID riid, void **out)
34 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
36 if (IsEqualGUID(riid, &IID_IDirect3DQuery9)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DQuery9_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI d3d9_query_AddRef(IDirect3DQuery9 *iface)
52 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
53 ULONG refcount = InterlockedIncrement(&query->refcount);
55 TRACE("%p increasing refcount to %u.\n", iface, refcount);
57 return refcount;
60 static ULONG WINAPI d3d9_query_Release(IDirect3DQuery9 *iface)
62 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
63 ULONG refcount = InterlockedDecrement(&query->refcount);
65 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
67 if (!refcount)
69 wined3d_mutex_lock();
70 wined3d_query_decref(query->wined3d_query);
71 wined3d_mutex_unlock();
73 IDirect3DDevice9Ex_Release(query->parent_device);
74 heap_free(query);
76 return refcount;
79 static HRESULT WINAPI d3d9_query_GetDevice(IDirect3DQuery9 *iface, IDirect3DDevice9 **device)
81 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
83 TRACE("iface %p, device %p.\n", iface, device);
85 *device = (IDirect3DDevice9 *)query->parent_device;
86 IDirect3DDevice9_AddRef(*device);
88 TRACE("Returning device %p.\n", *device);
90 return D3D_OK;
93 static D3DQUERYTYPE WINAPI d3d9_query_GetType(IDirect3DQuery9 *iface)
95 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
96 D3DQUERYTYPE type;
98 TRACE("iface %p.\n", iface);
100 wined3d_mutex_lock();
101 type = wined3d_query_get_type(query->wined3d_query);
102 wined3d_mutex_unlock();
104 return type;
107 static DWORD WINAPI d3d9_query_GetDataSize(IDirect3DQuery9 *iface)
109 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
111 TRACE("iface %p.\n", iface);
113 return query->data_size;
116 static HRESULT WINAPI d3d9_query_Issue(IDirect3DQuery9 *iface, DWORD flags)
118 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
119 HRESULT hr;
121 TRACE("iface %p, flags %#x.\n", iface, flags);
123 wined3d_mutex_lock();
124 hr = wined3d_query_issue(query->wined3d_query, flags);
125 wined3d_mutex_unlock();
127 return hr;
130 static HRESULT WINAPI d3d9_query_GetData(IDirect3DQuery9 *iface, void *data, DWORD size, DWORD flags)
132 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
133 enum wined3d_query_type type;
134 HRESULT hr;
136 TRACE("iface %p, data %p, size %u, flags %#x.\n",
137 iface, data, size, flags);
139 wined3d_mutex_lock();
140 type = wined3d_query_get_type(query->wined3d_query);
141 if (type == WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT && data)
143 struct wined3d_query_data_timestamp_disjoint data_disjoint;
145 if (size > sizeof(data_disjoint.disjoint))
146 size = sizeof(data_disjoint.disjoint);
148 hr = wined3d_query_get_data(query->wined3d_query, &data_disjoint, sizeof(data_disjoint), flags);
149 if (SUCCEEDED(hr))
150 memcpy(data, &data_disjoint.disjoint, size);
152 else
154 hr = wined3d_query_get_data(query->wined3d_query, data, size, flags);
156 wined3d_mutex_unlock();
158 if (hr == D3DERR_INVALIDCALL)
160 if (data)
162 memset(data, 0, size);
163 memset(data, 0xdd, min(size, query->data_size));
165 return S_OK;
167 return hr;
171 static const struct IDirect3DQuery9Vtbl d3d9_query_vtbl =
173 d3d9_query_QueryInterface,
174 d3d9_query_AddRef,
175 d3d9_query_Release,
176 d3d9_query_GetDevice,
177 d3d9_query_GetType,
178 d3d9_query_GetDataSize,
179 d3d9_query_Issue,
180 d3d9_query_GetData,
183 HRESULT query_init(struct d3d9_query *query, struct d3d9_device *device, D3DQUERYTYPE type)
185 HRESULT hr;
187 if (type > D3DQUERYTYPE_MEMORYPRESSURE)
189 if (type == 0x16)
190 FIXME("Undocumented query %#x created.\n", type);
191 else
192 WARN("Invalid query type %#x.\n", type);
194 return D3DERR_NOTAVAILABLE;
197 query->IDirect3DQuery9_iface.lpVtbl = &d3d9_query_vtbl;
198 query->refcount = 1;
200 wined3d_mutex_lock();
201 if (FAILED(hr = wined3d_query_create(device->wined3d_device, type,
202 query, &d3d9_null_wined3d_parent_ops, &query->wined3d_query)))
204 wined3d_mutex_unlock();
205 WARN("Failed to create wined3d query, hr %#x.\n", hr);
206 return hr;
209 if (type == D3DQUERYTYPE_OCCLUSION)
210 query->data_size = sizeof(DWORD);
211 else if (type == D3DQUERYTYPE_TIMESTAMPDISJOINT)
212 query->data_size = sizeof(BOOL);
213 else
214 query->data_size = wined3d_query_get_data_size(query->wined3d_query);
215 wined3d_mutex_unlock();
217 query->parent_device = &device->IDirect3DDevice9Ex_iface;
218 IDirect3DDevice9Ex_AddRef(query->parent_device);
220 return D3D_OK;