d3d9: Hold the lock in query methods.
[wine/gsoc_dplay.git] / dlls / d3d9 / query.c
bloba11aacecb6d27d81d70957bf12264875d40c0a5c
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 "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 /* IDirect3DQuery9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DQuery9Impl_QueryInterface(LPDIRECT3DQUERY9 iface, REFIID riid, LPVOID* ppobj) {
30 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
31 TRACE("(%p) Relay\n", This);
33 if (IsEqualGUID(riid, &IID_IUnknown)
34 || IsEqualGUID(riid, &IID_IDirect3DQuery9)) {
35 IUnknown_AddRef(iface);
36 *ppobj = This;
37 return S_OK;
40 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41 *ppobj = NULL;
42 return E_NOINTERFACE;
45 static ULONG WINAPI IDirect3DQuery9Impl_AddRef(LPDIRECT3DQUERY9 iface) {
46 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
47 ULONG ref = InterlockedIncrement(&This->ref);
49 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50 return ref;
53 static ULONG WINAPI IDirect3DQuery9Impl_Release(LPDIRECT3DQUERY9 iface) {
54 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
55 ULONG ref = InterlockedDecrement(&This->ref);
57 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59 if (ref == 0) {
60 IUnknown_Release(This->parentDevice);
61 HeapFree(GetProcessHeap(), 0, This);
63 return ref;
66 /* IDirect3DQuery9 Interface follow: */
67 static HRESULT WINAPI IDirect3DQuery9Impl_GetDevice(LPDIRECT3DQUERY9 iface, IDirect3DDevice9** ppDevice) {
68 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
69 IWineD3DDevice* pDevice;
70 HRESULT hr;
72 TRACE("(%p) Relay\n", This);
74 EnterCriticalSection(&d3d9_cs);
75 hr = IWineD3DQuery_GetDevice(This->wineD3DQuery, &pDevice);
76 if(hr != D3D_OK){
77 *ppDevice = NULL;
78 }else{
79 hr = IWineD3DDevice_GetParent(pDevice, (IUnknown **)ppDevice);
80 IWineD3DDevice_Release(pDevice);
82 LeaveCriticalSection(&d3d9_cs);
83 return hr;
86 static D3DQUERYTYPE WINAPI IDirect3DQuery9Impl_GetType(LPDIRECT3DQUERY9 iface) {
87 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
88 HRESULT hr;
89 TRACE("(%p) Relay\n", This);
91 EnterCriticalSection(&d3d9_cs);
92 hr = IWineD3DQuery_GetType(This->wineD3DQuery);
93 LeaveCriticalSection(&d3d9_cs);
94 return hr;
97 static DWORD WINAPI IDirect3DQuery9Impl_GetDataSize(LPDIRECT3DQUERY9 iface) {
98 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
99 DWORD ret;
100 TRACE("(%p) Relay\n", This);
102 EnterCriticalSection(&d3d9_cs);
103 ret = IWineD3DQuery_GetDataSize(This->wineD3DQuery);
104 LeaveCriticalSection(&d3d9_cs);
105 return ret;
108 static HRESULT WINAPI IDirect3DQuery9Impl_Issue(LPDIRECT3DQUERY9 iface, DWORD dwIssueFlags) {
109 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
110 HRESULT hr;
111 TRACE("(%p) Relay\n", This);
113 EnterCriticalSection(&d3d9_cs);
114 hr = IWineD3DQuery_Issue(This->wineD3DQuery, dwIssueFlags);
115 LeaveCriticalSection(&d3d9_cs);
116 return hr;
119 static HRESULT WINAPI IDirect3DQuery9Impl_GetData(LPDIRECT3DQUERY9 iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
120 IDirect3DQuery9Impl *This = (IDirect3DQuery9Impl *)iface;
121 HRESULT hr;
122 TRACE("(%p) Relay\n", This);
124 EnterCriticalSection(&d3d9_cs);
125 hr = IWineD3DQuery_GetData(This->wineD3DQuery, pData, dwSize, dwGetDataFlags);
126 LeaveCriticalSection(&d3d9_cs);
127 return hr;
131 static const IDirect3DQuery9Vtbl Direct3DQuery9_Vtbl =
133 IDirect3DQuery9Impl_QueryInterface,
134 IDirect3DQuery9Impl_AddRef,
135 IDirect3DQuery9Impl_Release,
136 IDirect3DQuery9Impl_GetDevice,
137 IDirect3DQuery9Impl_GetType,
138 IDirect3DQuery9Impl_GetDataSize,
139 IDirect3DQuery9Impl_Issue,
140 IDirect3DQuery9Impl_GetData
144 /* IDirect3DDevice9 IDirect3DQuery9 Methods follow: */
145 HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(LPDIRECT3DDEVICE9 iface, D3DQUERYTYPE Type, IDirect3DQuery9** ppQuery) {
146 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
147 IDirect3DQuery9Impl *object = NULL;
148 HRESULT hr = D3D_OK;
150 TRACE("(%p) Relay\n", This);
152 if (!ppQuery)
154 EnterCriticalSection(&d3d9_cs);
155 hr = IWineD3DDevice_CreateQuery(This->WineD3DDevice, Type, NULL, NULL);
156 LeaveCriticalSection(&d3d9_cs);
157 return hr;
160 /* Allocate the storage for the device */
161 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DQuery9Impl));
162 if (NULL == object) {
163 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
164 return D3DERR_OUTOFVIDEOMEMORY;
167 object->lpVtbl = &Direct3DQuery9_Vtbl;
168 object->ref = 1;
169 EnterCriticalSection(&d3d9_cs);
170 hr = IWineD3DDevice_CreateQuery(This->WineD3DDevice, Type, &object->wineD3DQuery, (IUnknown *)object);
171 LeaveCriticalSection(&d3d9_cs);
173 if (FAILED(hr)) {
175 /* free up object */
176 FIXME("(%p) call to IWineD3DDevice_CreateQuery failed\n", This);
177 HeapFree(GetProcessHeap(), 0, object);
178 } else {
179 IUnknown_AddRef(iface);
180 object->parentDevice = iface;
181 *ppQuery = (LPDIRECT3DQUERY9) object;
182 TRACE("(%p) : Created query %p\n", This , object);
184 TRACE("(%p) : returning %x\n", This, hr);
185 return hr;