Sync with trunk rev.61910 to get latest improvements and bugfixes.
[reactos.git] / dll / directx / wine / d3d9 / volume.c
1 /*
2 * IDirect3DVolume9 implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "d3d9_private.h"
23
24 static inline struct d3d9_volume *impl_from_IDirect3DVolume9(IDirect3DVolume9 *iface)
25 {
26 return CONTAINING_RECORD(iface, struct d3d9_volume, IDirect3DVolume9_iface);
27 }
28
29 static HRESULT WINAPI d3d9_volume_QueryInterface(IDirect3DVolume9 *iface, REFIID riid, void **out)
30 {
31 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
32
33 if (IsEqualGUID(riid, &IID_IDirect3DVolume9)
34 || IsEqualGUID(riid, &IID_IUnknown))
35 {
36 IDirect3DVolume9_AddRef(iface);
37 *out = iface;
38 return S_OK;
39 }
40
41 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
42
43 *out = NULL;
44 return E_NOINTERFACE;
45 }
46
47 static ULONG WINAPI d3d9_volume_AddRef(IDirect3DVolume9 *iface)
48 {
49 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
50 ULONG refcount;
51
52 TRACE("iface %p.\n", iface);
53
54 if (volume->forwardReference)
55 {
56 TRACE("Forwarding to %p.\n", volume->forwardReference);
57 return IUnknown_AddRef(volume->forwardReference);
58 }
59
60 refcount = InterlockedIncrement(&volume->refcount);
61 TRACE("%p increasing refcount to %u.\n", iface, refcount);
62
63 if (refcount == 1)
64 {
65 wined3d_mutex_lock();
66 wined3d_volume_incref(volume->wined3d_volume);
67 wined3d_mutex_unlock();
68 }
69
70 return refcount;
71 }
72
73 static ULONG WINAPI d3d9_volume_Release(IDirect3DVolume9 *iface)
74 {
75 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
76 ULONG refcount;
77
78 TRACE("iface %p.\n", iface);
79
80 if (volume->forwardReference)
81 {
82 TRACE("Forwarding to %p.\n", volume->forwardReference);
83 return IUnknown_Release(volume->forwardReference);
84 }
85
86 refcount = InterlockedDecrement(&volume->refcount);
87 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
88
89 if (!refcount)
90 {
91 wined3d_mutex_lock();
92 wined3d_volume_decref(volume->wined3d_volume);
93 wined3d_mutex_unlock();
94 }
95
96 return refcount;
97 }
98
99 static HRESULT WINAPI d3d9_volume_GetDevice(IDirect3DVolume9 *iface, IDirect3DDevice9 **device)
100 {
101 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
102 IDirect3DResource9 *resource;
103 HRESULT hr;
104
105 TRACE("iface %p, device %p.\n", iface, device);
106
107 hr = IUnknown_QueryInterface(volume->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
108 if (SUCCEEDED(hr))
109 {
110 hr = IDirect3DResource9_GetDevice(resource, device);
111 IDirect3DResource9_Release(resource);
112
113 TRACE("Returning device %p.\n", *device);
114 }
115
116 return hr;
117 }
118
119 static HRESULT WINAPI d3d9_volume_SetPrivateData(IDirect3DVolume9 *iface, REFGUID guid,
120 const void *data, DWORD data_size, DWORD flags)
121 {
122 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
123 struct wined3d_resource *resource;
124 HRESULT hr;
125
126 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
127 iface, debugstr_guid(guid), data, data_size, flags);
128
129 wined3d_mutex_lock();
130 resource = wined3d_volume_get_resource(volume->wined3d_volume);
131 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
132 wined3d_mutex_unlock();
133
134 return hr;
135 }
136
137 static HRESULT WINAPI d3d9_volume_GetPrivateData(IDirect3DVolume9 *iface, REFGUID guid,
138 void *data, DWORD *data_size)
139 {
140 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
141 struct wined3d_resource *resource;
142 HRESULT hr;
143
144 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
145 iface, debugstr_guid(guid), data, data_size);
146
147 wined3d_mutex_lock();
148 resource = wined3d_volume_get_resource(volume->wined3d_volume);
149 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
150 wined3d_mutex_unlock();
151
152 return hr;
153 }
154
155 static HRESULT WINAPI d3d9_volume_FreePrivateData(IDirect3DVolume9 *iface, REFGUID guid)
156 {
157 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
158 struct wined3d_resource *resource;
159 HRESULT hr;
160
161 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
162
163 wined3d_mutex_lock();
164 resource = wined3d_volume_get_resource(volume->wined3d_volume);
165 hr = wined3d_resource_free_private_data(resource, guid);
166 wined3d_mutex_unlock();
167
168 return hr;
169 }
170
171 static HRESULT WINAPI d3d9_volume_GetContainer(IDirect3DVolume9 *iface, REFIID riid, void **container)
172 {
173 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
174 HRESULT hr;
175
176 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
177
178 if (!volume->container)
179 return E_NOINTERFACE;
180
181 hr = IUnknown_QueryInterface(volume->container, riid, container);
182
183 TRACE("Returning %p,\n", *container);
184
185 return hr;
186 }
187
188 static HRESULT WINAPI d3d9_volume_GetDesc(IDirect3DVolume9 *iface, D3DVOLUME_DESC *desc)
189 {
190 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
191 struct wined3d_resource_desc wined3d_desc;
192 struct wined3d_resource *wined3d_resource;
193
194 TRACE("iface %p, desc %p.\n", iface, desc);
195
196 wined3d_mutex_lock();
197 wined3d_resource = wined3d_volume_get_resource(volume->wined3d_volume);
198 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
199 wined3d_mutex_unlock();
200
201 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
202 desc->Type = wined3d_desc.resource_type;
203 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
204 desc->Pool = wined3d_desc.pool;
205 desc->Width = wined3d_desc.width;
206 desc->Height = wined3d_desc.height;
207 desc->Depth = wined3d_desc.depth;
208
209 return D3D_OK;
210 }
211
212 static HRESULT WINAPI d3d9_volume_LockBox(IDirect3DVolume9 *iface,
213 D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
214 {
215 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
216 struct wined3d_map_desc map_desc;
217 HRESULT hr;
218
219 TRACE("iface %p, locked_box %p, box %p, flags %#x.\n",
220 iface, locked_box, box, flags);
221
222 wined3d_mutex_lock();
223 hr = wined3d_volume_map(volume->wined3d_volume, &map_desc, (const struct wined3d_box *)box, flags);
224 wined3d_mutex_unlock();
225
226 locked_box->RowPitch = map_desc.row_pitch;
227 locked_box->SlicePitch = map_desc.slice_pitch;
228 locked_box->pBits = map_desc.data;
229
230 return hr;
231 }
232
233 static HRESULT WINAPI d3d9_volume_UnlockBox(IDirect3DVolume9 *iface)
234 {
235 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
236 HRESULT hr;
237
238 TRACE("iface %p.\n", iface);
239
240 wined3d_mutex_lock();
241 hr = wined3d_volume_unmap(volume->wined3d_volume);
242 wined3d_mutex_unlock();
243
244 return hr;
245 }
246
247 static const struct IDirect3DVolume9Vtbl d3d9_volume_vtbl =
248 {
249 /* IUnknown */
250 d3d9_volume_QueryInterface,
251 d3d9_volume_AddRef,
252 d3d9_volume_Release,
253 /* IDirect3DVolume9 */
254 d3d9_volume_GetDevice,
255 d3d9_volume_SetPrivateData,
256 d3d9_volume_GetPrivateData,
257 d3d9_volume_FreePrivateData,
258 d3d9_volume_GetContainer,
259 d3d9_volume_GetDesc,
260 d3d9_volume_LockBox,
261 d3d9_volume_UnlockBox,
262 };
263
264 static void STDMETHODCALLTYPE volume_wined3d_object_destroyed(void *parent)
265 {
266 HeapFree(GetProcessHeap(), 0, parent);
267 }
268
269 static const struct wined3d_parent_ops d3d9_volume_wined3d_parent_ops =
270 {
271 volume_wined3d_object_destroyed,
272 };
273
274 void volume_init(struct d3d9_volume *volume, struct wined3d_volume *wined3d_volume,
275 const struct wined3d_parent_ops **parent_ops)
276 {
277 volume->IDirect3DVolume9_iface.lpVtbl = &d3d9_volume_vtbl;
278 volume->refcount = 1;
279 wined3d_volume_incref(wined3d_volume);
280 volume->wined3d_volume = wined3d_volume;
281
282 *parent_ops = &d3d9_volume_wined3d_parent_ops;
283 }