usp10: Support GPOS extension lookup subtables.
[wine] / dlls / 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 "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 static inline struct d3d9_volume *impl_from_IDirect3DVolume9(IDirect3DVolume9 *iface)
28 {
29     return CONTAINING_RECORD(iface, struct d3d9_volume, IDirect3DVolume9_iface);
30 }
31
32 static HRESULT WINAPI d3d9_volume_QueryInterface(IDirect3DVolume9 *iface, REFIID riid, void **out)
33 {
34     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
35
36     if (IsEqualGUID(riid, &IID_IDirect3DVolume9)
37             || IsEqualGUID(riid, &IID_IUnknown))
38     {
39         IDirect3DVolume9_AddRef(iface);
40         *out = iface;
41         return S_OK;
42     }
43
44     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
45
46     *out = NULL;
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI d3d9_volume_AddRef(IDirect3DVolume9 *iface)
51 {
52     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
53     ULONG refcount;
54
55     TRACE("iface %p.\n", iface);
56
57     if (volume->forwardReference)
58     {
59         TRACE("Forwarding to %p.\n", volume->forwardReference);
60         return IUnknown_AddRef(volume->forwardReference);
61     }
62
63     refcount = InterlockedIncrement(&volume->refcount);
64     TRACE("%p increasing refcount to %u.\n", iface, refcount);
65
66     if (refcount == 1)
67     {
68         wined3d_mutex_lock();
69         wined3d_volume_incref(volume->wined3d_volume);
70         wined3d_mutex_unlock();
71     }
72
73     return refcount;
74 }
75
76 static ULONG WINAPI d3d9_volume_Release(IDirect3DVolume9 *iface)
77 {
78     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
79     ULONG refcount;
80
81     TRACE("iface %p.\n", iface);
82
83     if (volume->forwardReference)
84     {
85         TRACE("Forwarding to %p.\n", volume->forwardReference);
86         return IUnknown_Release(volume->forwardReference);
87     }
88
89     refcount = InterlockedDecrement(&volume->refcount);
90     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
91
92     if (!refcount)
93     {
94         wined3d_mutex_lock();
95         wined3d_volume_decref(volume->wined3d_volume);
96         wined3d_mutex_unlock();
97     }
98
99     return refcount;
100 }
101
102 static HRESULT WINAPI d3d9_volume_GetDevice(IDirect3DVolume9 *iface, IDirect3DDevice9 **device)
103 {
104     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
105     IDirect3DResource9 *resource;
106     HRESULT hr;
107
108     TRACE("iface %p, device %p.\n", iface, device);
109
110     hr = IUnknown_QueryInterface(volume->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
111     if (SUCCEEDED(hr))
112     {
113         hr = IDirect3DResource9_GetDevice(resource, device);
114         IDirect3DResource9_Release(resource);
115
116         TRACE("Returning device %p.\n", *device);
117     }
118
119     return hr;
120 }
121
122 static HRESULT WINAPI d3d9_volume_SetPrivateData(IDirect3DVolume9 *iface, REFGUID guid,
123         const void *data, DWORD data_size, DWORD flags)
124 {
125     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
126     struct wined3d_resource *resource;
127     HRESULT hr;
128
129     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
130             iface, debugstr_guid(guid), data, data_size, flags);
131
132     wined3d_mutex_lock();
133     resource = wined3d_volume_get_resource(volume->wined3d_volume);
134     hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
135     wined3d_mutex_unlock();
136
137     return hr;
138 }
139
140 static HRESULT WINAPI d3d9_volume_GetPrivateData(IDirect3DVolume9 *iface, REFGUID guid,
141         void *data, DWORD *data_size)
142 {
143     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
144     struct wined3d_resource *resource;
145     HRESULT hr;
146
147     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
148             iface, debugstr_guid(guid), data, data_size);
149
150     wined3d_mutex_lock();
151     resource = wined3d_volume_get_resource(volume->wined3d_volume);
152     hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
153     wined3d_mutex_unlock();
154
155     return hr;
156 }
157
158 static HRESULT WINAPI d3d9_volume_FreePrivateData(IDirect3DVolume9 *iface, REFGUID guid)
159 {
160     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
161     struct wined3d_resource *resource;
162     HRESULT hr;
163
164     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
165
166     wined3d_mutex_lock();
167     resource = wined3d_volume_get_resource(volume->wined3d_volume);
168     hr = wined3d_resource_free_private_data(resource, guid);
169     wined3d_mutex_unlock();
170
171     return hr;
172 }
173
174 static HRESULT WINAPI d3d9_volume_GetContainer(IDirect3DVolume9 *iface, REFIID riid, void **container)
175 {
176     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
177     HRESULT hr;
178
179     TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
180
181     if (!volume->container)
182         return E_NOINTERFACE;
183
184     hr = IUnknown_QueryInterface(volume->container, riid, container);
185
186     TRACE("Returning %p,\n", *container);
187
188     return hr;
189 }
190
191 static HRESULT WINAPI d3d9_volume_GetDesc(IDirect3DVolume9 *iface, D3DVOLUME_DESC *desc)
192 {
193     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
194     struct wined3d_resource_desc wined3d_desc;
195     struct wined3d_resource *wined3d_resource;
196
197     TRACE("iface %p, desc %p.\n", iface, desc);
198
199     wined3d_mutex_lock();
200     wined3d_resource = wined3d_volume_get_resource(volume->wined3d_volume);
201     wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
202     wined3d_mutex_unlock();
203
204     desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
205     desc->Type = wined3d_desc.resource_type;
206     desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
207     desc->Pool = wined3d_desc.pool;
208     desc->Width = wined3d_desc.width;
209     desc->Height = wined3d_desc.height;
210     desc->Depth = wined3d_desc.depth;
211
212     return D3D_OK;
213 }
214
215 static HRESULT WINAPI d3d9_volume_LockBox(IDirect3DVolume9 *iface,
216         D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
217 {
218     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
219     struct wined3d_map_desc map_desc;
220     HRESULT hr;
221
222     TRACE("iface %p, locked_box %p, box %p, flags %#x.\n",
223             iface, locked_box, box, flags);
224
225     wined3d_mutex_lock();
226     hr = wined3d_volume_map(volume->wined3d_volume, &map_desc, (const struct wined3d_box *)box, flags);
227     wined3d_mutex_unlock();
228
229     locked_box->RowPitch = map_desc.row_pitch;
230     locked_box->SlicePitch = map_desc.slice_pitch;
231     locked_box->pBits = map_desc.data;
232
233     return hr;
234 }
235
236 static HRESULT WINAPI d3d9_volume_UnlockBox(IDirect3DVolume9 *iface)
237 {
238     struct d3d9_volume *volume = impl_from_IDirect3DVolume9(iface);
239     HRESULT hr;
240
241     TRACE("iface %p.\n", iface);
242
243     wined3d_mutex_lock();
244     hr = wined3d_volume_unmap(volume->wined3d_volume);
245     wined3d_mutex_unlock();
246
247     return hr;
248 }
249
250 static const struct IDirect3DVolume9Vtbl d3d9_volume_vtbl =
251 {
252     /* IUnknown */
253     d3d9_volume_QueryInterface,
254     d3d9_volume_AddRef,
255     d3d9_volume_Release,
256     /* IDirect3DVolume9 */
257     d3d9_volume_GetDevice,
258     d3d9_volume_SetPrivateData,
259     d3d9_volume_GetPrivateData,
260     d3d9_volume_FreePrivateData,
261     d3d9_volume_GetContainer,
262     d3d9_volume_GetDesc,
263     d3d9_volume_LockBox,
264     d3d9_volume_UnlockBox,
265 };
266
267 static void STDMETHODCALLTYPE volume_wined3d_object_destroyed(void *parent)
268 {
269     HeapFree(GetProcessHeap(), 0, parent);
270 }
271
272 static const struct wined3d_parent_ops d3d9_volume_wined3d_parent_ops =
273 {
274     volume_wined3d_object_destroyed,
275 };
276
277 HRESULT volume_init(struct d3d9_volume *volume, struct d3d9_device *device, UINT width, UINT height,
278         UINT depth, DWORD usage, enum wined3d_format_id format, enum wined3d_pool pool)
279 {
280     HRESULT hr;
281
282     volume->IDirect3DVolume9_iface.lpVtbl = &d3d9_volume_vtbl;
283     volume->refcount = 1;
284
285     hr = wined3d_volume_create(device->wined3d_device, width, height, depth, usage & WINED3DUSAGE_MASK,
286             format, pool, volume, &d3d9_volume_wined3d_parent_ops, &volume->wined3d_volume);
287     if (FAILED(hr))
288     {
289         WARN("Failed to create wined3d volume, hr %#x.\n", hr);
290         return hr;
291     }
292
293     return D3D_OK;
294 }