wined3d: Remove object creation methods from the IWineD3DDevice interface.
[wine] / dlls / 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 "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 static inline IDirect3DVolume8Impl *impl_from_IDirect3DVolume8(IDirect3DVolume8 *iface)
27 {
28     return CONTAINING_RECORD(iface, IDirect3DVolume8Impl, IDirect3DVolume8_iface);
29 }
30
31 static HRESULT WINAPI IDirect3DVolume8Impl_QueryInterface(IDirect3DVolume8 *iface, REFIID riid,
32         void **ppobj)
33 {
34     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
35
36     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
37
38     if (IsEqualGUID(riid, &IID_IUnknown)
39         || IsEqualGUID(riid, &IID_IDirect3DVolume8)) {
40         IUnknown_AddRef(iface);
41         *ppobj = This;
42         return S_OK;
43     }
44
45     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
46     *ppobj = NULL;
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI IDirect3DVolume8Impl_AddRef(IDirect3DVolume8 *iface)
51 {
52     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
53
54     TRACE("iface %p.\n", iface);
55
56     if (This->forwardReference) {
57         /* Forward to the containerParent */
58         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
59         return IUnknown_AddRef(This->forwardReference);
60     } else {
61         /* No container, handle our own refcounting */
62         ULONG ref = InterlockedIncrement(&This->ref);
63
64         TRACE("%p increasing refcount to %u.\n", iface, ref);
65
66         if (ref == 1)
67         {
68             wined3d_mutex_lock();
69             wined3d_volume_incref(This->wined3d_volume);
70             wined3d_mutex_unlock();
71         }
72
73         return ref;
74     }
75 }
76
77 static ULONG WINAPI IDirect3DVolume8Impl_Release(IDirect3DVolume8 *iface)
78 {
79     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
80
81     TRACE("iface %p.\n", iface);
82
83     if (This->forwardReference) {
84         /* Forward to the containerParent */
85         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
86         return IUnknown_Release(This->forwardReference);
87     }
88     else {
89         /* No container, handle our own refcounting */
90         ULONG ref = InterlockedDecrement(&This->ref);
91
92         TRACE("%p decreasing refcount to %u.\n", iface, ref);
93
94         if (ref == 0) {
95             wined3d_mutex_lock();
96             wined3d_volume_decref(This->wined3d_volume);
97             wined3d_mutex_unlock();
98         }
99
100         return ref;
101     }
102 }
103
104 static HRESULT WINAPI IDirect3DVolume8Impl_GetDevice(IDirect3DVolume8 *iface,
105         IDirect3DDevice8 **device)
106 {
107     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
108     IDirect3DResource8 *resource;
109     HRESULT hr;
110
111     TRACE("iface %p, device %p.\n", iface, device);
112
113     hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
114     if (SUCCEEDED(hr))
115     {
116         hr = IDirect3DResource8_GetDevice(resource, device);
117         IDirect3DResource8_Release(resource);
118
119         TRACE("Returning device %p.\n", *device);
120     }
121
122     return hr;
123 }
124
125 static HRESULT WINAPI IDirect3DVolume8Impl_SetPrivateData(IDirect3DVolume8 *iface, REFGUID refguid,
126         const void *pData, DWORD SizeOfData, DWORD Flags)
127 {
128     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
129     HRESULT hr;
130
131     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
132             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
133
134     wined3d_mutex_lock();
135     hr = wined3d_volume_set_private_data(This->wined3d_volume, refguid, pData, SizeOfData, Flags);
136     wined3d_mutex_unlock();
137
138     return hr;
139 }
140
141 static HRESULT WINAPI IDirect3DVolume8Impl_GetPrivateData(IDirect3DVolume8 *iface, REFGUID  refguid,
142         void *pData, DWORD *pSizeOfData)
143 {
144     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
145     HRESULT hr;
146
147     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
148             iface, debugstr_guid(refguid), pData, pSizeOfData);
149
150     wined3d_mutex_lock();
151     hr = wined3d_volume_get_private_data(This->wined3d_volume, refguid, pData, pSizeOfData);
152     wined3d_mutex_unlock();
153
154     return hr;
155 }
156
157 static HRESULT WINAPI IDirect3DVolume8Impl_FreePrivateData(IDirect3DVolume8 *iface, REFGUID refguid)
158 {
159     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
160     HRESULT hr;
161
162     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
163
164     wined3d_mutex_lock();
165     hr = wined3d_volume_free_private_data(This->wined3d_volume, refguid);
166     wined3d_mutex_unlock();
167
168     return hr;
169 }
170
171 static HRESULT WINAPI IDirect3DVolume8Impl_GetContainer(IDirect3DVolume8 *iface, REFIID riid,
172         void **ppContainer)
173 {
174     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
175     HRESULT res;
176
177     TRACE("iface %p, riid %s, container %p.\n",
178             iface, debugstr_guid(riid), ppContainer);
179
180     if (!This->container) return E_NOINTERFACE;
181
182     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
183
184     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
185
186     return res;
187 }
188
189 static HRESULT WINAPI IDirect3DVolume8Impl_GetDesc(IDirect3DVolume8 *iface, D3DVOLUME_DESC *desc)
190 {
191     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
192     struct wined3d_resource_desc wined3d_desc;
193     struct wined3d_resource *wined3d_resource;
194
195     TRACE("iface %p, desc %p.\n", iface, desc);
196
197     wined3d_mutex_lock();
198     wined3d_resource = wined3d_volume_get_resource(This->wined3d_volume);
199     wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
200     wined3d_mutex_unlock();
201
202     desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
203     desc->Type = wined3d_desc.resource_type;
204     desc->Usage = wined3d_desc.usage;
205     desc->Pool = wined3d_desc.pool;
206     desc->Size = wined3d_desc.size;
207     desc->Width = wined3d_desc.width;
208     desc->Height = wined3d_desc.height;
209     desc->Depth = wined3d_desc.depth;
210
211     return D3D_OK;
212 }
213
214 static HRESULT WINAPI IDirect3DVolume8Impl_LockBox(IDirect3DVolume8 *iface,
215         D3DLOCKED_BOX *pLockedVolume, const D3DBOX *pBox, DWORD Flags)
216 {
217     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
218     HRESULT hr;
219
220     TRACE("iface %p, locked_box %p, box %p, flags %#x.\n",
221             iface, pLockedVolume, pBox, Flags);
222
223     wined3d_mutex_lock();
224     hr = wined3d_volume_map(This->wined3d_volume, (WINED3DLOCKED_BOX *)pLockedVolume,
225             (const WINED3DBOX *)pBox, Flags);
226     wined3d_mutex_unlock();
227
228     return hr;
229 }
230
231 static HRESULT WINAPI IDirect3DVolume8Impl_UnlockBox(IDirect3DVolume8 *iface)
232 {
233     IDirect3DVolume8Impl *This = impl_from_IDirect3DVolume8(iface);
234     HRESULT hr;
235
236     TRACE("iface %p.\n", iface);
237
238     wined3d_mutex_lock();
239     hr = wined3d_volume_unmap(This->wined3d_volume);
240     wined3d_mutex_unlock();
241
242     return hr;
243 }
244
245 static const IDirect3DVolume8Vtbl Direct3DVolume8_Vtbl =
246 {
247     /* IUnknown */
248     IDirect3DVolume8Impl_QueryInterface,
249     IDirect3DVolume8Impl_AddRef,
250     IDirect3DVolume8Impl_Release,
251     /* IDirect3DVolume8 */
252     IDirect3DVolume8Impl_GetDevice,
253     IDirect3DVolume8Impl_SetPrivateData,
254     IDirect3DVolume8Impl_GetPrivateData,
255     IDirect3DVolume8Impl_FreePrivateData,
256     IDirect3DVolume8Impl_GetContainer,
257     IDirect3DVolume8Impl_GetDesc,
258     IDirect3DVolume8Impl_LockBox,
259     IDirect3DVolume8Impl_UnlockBox
260 };
261
262 static void STDMETHODCALLTYPE volume_wined3d_object_destroyed(void *parent)
263 {
264     HeapFree(GetProcessHeap(), 0, parent);
265 }
266
267 static const struct wined3d_parent_ops d3d8_volume_wined3d_parent_ops =
268 {
269     volume_wined3d_object_destroyed,
270 };
271
272 HRESULT volume_init(IDirect3DVolume8Impl *volume, IDirect3DDevice8Impl *device, UINT width, UINT height,
273         UINT depth, DWORD usage, enum wined3d_format_id format, WINED3DPOOL pool)
274 {
275     HRESULT hr;
276
277     volume->IDirect3DVolume8_iface.lpVtbl = &Direct3DVolume8_Vtbl;
278     volume->ref = 1;
279
280     hr = wined3d_volume_create(device->WineD3DDevice, width, height, depth, usage,
281             format, pool, volume, &d3d8_volume_wined3d_parent_ops, &volume->wined3d_volume);
282     if (FAILED(hr))
283     {
284         WARN("Failed to create wined3d volume, hr %#x.\n", hr);
285         return hr;
286     }
287
288     return D3D_OK;
289 }