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