Sync with trunk revision 63128.
[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->resource.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->resource.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 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
124 iface, debugstr_guid(guid), data, data_size, flags);
125
126 return d3d9_resource_set_private_data(&volume->resource, guid, data, data_size, flags);
127 }
128
129 static HRESULT WINAPI d3d9_volume_GetPrivateData(IDirect3DVolume9 *iface, REFGUID guid,
130 void *data, DWORD *data_size)
131 {
132 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
133 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
134 iface, debugstr_guid(guid), data, data_size);
135
136 return d3d9_resource_get_private_data(&volume->resource, guid, data, data_size);
137 }
138
139 static HRESULT WINAPI d3d9_volume_FreePrivateData(IDirect3DVolume9 *iface, REFGUID guid)
140 {
141 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
142 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
143
144 return d3d9_resource_free_private_data(&volume->resource, guid);
145 }
146
147 static HRESULT WINAPI d3d9_volume_GetContainer(IDirect3DVolume9 *iface, REFIID riid, void **container)
148 {
149 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
150 HRESULT hr;
151
152 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
153
154 if (!volume->container)
155 return E_NOINTERFACE;
156
157 hr = IUnknown_QueryInterface(volume->container, riid, container);
158
159 TRACE("Returning %p,\n", *container);
160
161 return hr;
162 }
163
164 static HRESULT WINAPI d3d9_volume_GetDesc(IDirect3DVolume9 *iface, D3DVOLUME_DESC *desc)
165 {
166 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
167 struct wined3d_resource_desc wined3d_desc;
168 struct wined3d_resource *wined3d_resource;
169
170 TRACE("iface %p, desc %p.\n", iface, desc);
171
172 wined3d_mutex_lock();
173 wined3d_resource = wined3d_volume_get_resource(volume->wined3d_volume);
174 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
175 wined3d_mutex_unlock();
176
177 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
178 desc->Type = wined3d_desc.resource_type;
179 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
180 desc->Pool = wined3d_desc.pool;
181 desc->Width = wined3d_desc.width;
182 desc->Height = wined3d_desc.height;
183 desc->Depth = wined3d_desc.depth;
184
185 return D3D_OK;
186 }
187
188 static HRESULT WINAPI d3d9_volume_LockBox(IDirect3DVolume9 *iface,
189 D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
190 {
191 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
192 struct wined3d_map_desc map_desc;
193 HRESULT hr;
194
195 TRACE("iface %p, locked_box %p, box %p, flags %#x.\n",
196 iface, locked_box, box, flags);
197
198 wined3d_mutex_lock();
199 hr = wined3d_volume_map(volume->wined3d_volume, &map_desc, (const struct wined3d_box *)box, flags);
200 wined3d_mutex_unlock();
201
202 locked_box->RowPitch = map_desc.row_pitch;
203 locked_box->SlicePitch = map_desc.slice_pitch;
204 locked_box->pBits = map_desc.data;
205
206 return hr;
207 }
208
209 static HRESULT WINAPI d3d9_volume_UnlockBox(IDirect3DVolume9 *iface)
210 {
211 struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
212 HRESULT hr;
213
214 TRACE("iface %p.\n", iface);
215
216 wined3d_mutex_lock();
217 hr = wined3d_volume_unmap(volume->wined3d_volume);
218 wined3d_mutex_unlock();
219
220 return hr;
221 }
222
223 static const struct IDirect3DVolume9Vtbl d3d9_volume_vtbl =
224 {
225 /* IUnknown */
226 d3d9_volume_QueryInterface,
227 d3d9_volume_AddRef,
228 d3d9_volume_Release,
229 /* IDirect3DVolume9 */
230 d3d9_volume_GetDevice,
231 d3d9_volume_SetPrivateData,
232 d3d9_volume_GetPrivateData,
233 d3d9_volume_FreePrivateData,
234 d3d9_volume_GetContainer,
235 d3d9_volume_GetDesc,
236 d3d9_volume_LockBox,
237 d3d9_volume_UnlockBox,
238 };
239
240 static void STDMETHODCALLTYPE volume_wined3d_object_destroyed(void *parent)
241 {
242 struct d3d9_volume *volume = parent;
243 d3d9_resource_cleanup(&volume->resource);
244 HeapFree(GetProcessHeap(), 0, volume);
245 }
246
247 static const struct wined3d_parent_ops d3d9_volume_wined3d_parent_ops =
248 {
249 volume_wined3d_object_destroyed,
250 };
251
252 void volume_init(struct d3d9_volume *volume, struct wined3d_volume *wined3d_volume,
253 const struct wined3d_parent_ops **parent_ops)
254 {
255 volume->IDirect3DVolume9_iface.lpVtbl = &d3d9_volume_vtbl;
256 d3d9_resource_init(&volume->resource);
257 wined3d_volume_incref(wined3d_volume);
258 volume->wined3d_volume = wined3d_volume;
259
260 *parent_ops = &d3d9_volume_wined3d_parent_ops;
261 }