mmdevapi/tests: Add tests for volume control interfaces.
[wine] / dlls / d3d9 / cubetexture.c
1 /*
2  * IDirect3DCubeTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28
29 static inline IDirect3DCubeTexture9Impl *impl_from_IDirect3DCubeTexture9(IDirect3DCubeTexture9 *iface)
30 {
31     return CONTAINING_RECORD(iface, IDirect3DCubeTexture9Impl, IDirect3DCubeTexture9_iface);
32 }
33
34 static HRESULT WINAPI IDirect3DCubeTexture9Impl_QueryInterface(IDirect3DCubeTexture9 *iface,
35         REFIID riid, void **ppobj)
36 {
37     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
38
39     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
40
41     if (IsEqualGUID(riid, &IID_IUnknown)
42         || IsEqualGUID(riid, &IID_IDirect3DResource9)
43         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
44         || IsEqualGUID(riid, &IID_IDirect3DCubeTexture9)) {
45         IDirect3DCubeTexture9_AddRef(iface);
46         *ppobj = This;
47         return S_OK;
48     }
49
50     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
51     *ppobj = NULL;
52     return E_NOINTERFACE;
53 }
54
55 static ULONG WINAPI IDirect3DCubeTexture9Impl_AddRef(IDirect3DCubeTexture9 *iface)
56 {
57     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
58     ULONG ref = InterlockedIncrement(&This->ref);
59
60     TRACE("%p increasing refcount to %u.\n", iface, ref);
61
62     if (ref == 1)
63     {
64         IDirect3DDevice9Ex_AddRef(This->parentDevice);
65         wined3d_mutex_lock();
66         wined3d_texture_incref(This->wined3d_texture);
67         wined3d_mutex_unlock();
68     }
69
70     return ref;
71 }
72
73 static ULONG WINAPI IDirect3DCubeTexture9Impl_Release(IDirect3DCubeTexture9 *iface)
74 {
75     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
76     ULONG ref = InterlockedDecrement(&This->ref);
77
78     TRACE("%p decreasing refcount to %u.\n", iface, ref);
79
80     if (ref == 0) {
81         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
82
83         TRACE("Releasing child %p.\n", This->wined3d_texture);
84
85         wined3d_mutex_lock();
86         wined3d_texture_decref(This->wined3d_texture);
87         wined3d_mutex_unlock();
88
89         /* Release the device last, as it may cause the device to be destroyed. */
90         IDirect3DDevice9Ex_Release(parentDevice);
91     }
92     return ref;
93 }
94
95 /* IDirect3DCubeTexture9 IDirect3DResource9 Interface follow: */
96 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetDevice(IDirect3DCubeTexture9 *iface,
97         IDirect3DDevice9 **device)
98 {
99     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
100
101     TRACE("iface %p, device %p.\n", iface, device);
102
103     *device = (IDirect3DDevice9 *)This->parentDevice;
104     IDirect3DDevice9_AddRef(*device);
105
106     TRACE("Returning device %p.\n", *device);
107
108     return D3D_OK;
109 }
110
111 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetPrivateData(IDirect3DCubeTexture9 *iface,
112         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
113 {
114     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
115     HRESULT hr;
116
117     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
118             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
119
120     wined3d_mutex_lock();
121     hr = wined3d_texture_set_private_data(This->wined3d_texture, refguid, pData, SizeOfData, Flags);
122     wined3d_mutex_unlock();
123
124     return hr;
125 }
126
127 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetPrivateData(IDirect3DCubeTexture9 *iface,
128         REFGUID refguid, void *pData, DWORD *pSizeOfData)
129 {
130     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
131     HRESULT hr;
132
133     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
134             iface, debugstr_guid(refguid), pData, pSizeOfData);
135
136     wined3d_mutex_lock();
137     hr = wined3d_texture_get_private_data(This->wined3d_texture, refguid, pData, pSizeOfData);
138     wined3d_mutex_unlock();
139
140     return hr;
141 }
142
143 static HRESULT WINAPI IDirect3DCubeTexture9Impl_FreePrivateData(IDirect3DCubeTexture9 *iface,
144         REFGUID refguid)
145 {
146     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
147     HRESULT hr;
148
149     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
150
151     wined3d_mutex_lock();
152     hr = wined3d_texture_free_private_data(This->wined3d_texture, refguid);
153     wined3d_mutex_unlock();
154
155     return hr;
156 }
157
158 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetPriority(IDirect3DCubeTexture9 *iface,
159         DWORD PriorityNew)
160 {
161     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
162     DWORD ret;
163
164     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
165
166     wined3d_mutex_lock();
167     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
168     wined3d_mutex_unlock();
169
170     return ret;
171 }
172
173 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetPriority(IDirect3DCubeTexture9 *iface)
174 {
175     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
176     DWORD ret;
177
178     TRACE("iface %p.\n", iface);
179
180     wined3d_mutex_lock();
181     ret = wined3d_texture_get_priority(This->wined3d_texture);
182     wined3d_mutex_unlock();
183
184     return ret;
185 }
186
187 static void WINAPI IDirect3DCubeTexture9Impl_PreLoad(IDirect3DCubeTexture9 *iface)
188 {
189     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
190
191     TRACE("iface %p.\n", iface);
192
193     wined3d_mutex_lock();
194     wined3d_texture_preload(This->wined3d_texture);
195     wined3d_mutex_unlock();
196 }
197
198 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture9Impl_GetType(IDirect3DCubeTexture9 *iface)
199 {
200     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
201     D3DRESOURCETYPE ret;
202
203     TRACE("iface %p.\n", iface);
204
205     wined3d_mutex_lock();
206     ret = wined3d_texture_get_type(This->wined3d_texture);
207     wined3d_mutex_unlock();
208
209     return ret;
210 }
211
212 /* IDirect3DCubeTexture9 IDirect3DBaseTexture9 Interface follow: */
213 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetLOD(IDirect3DCubeTexture9 *iface, DWORD LODNew)
214 {
215     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
216     DWORD ret;
217
218     TRACE("iface %p, lod %u.\n", iface, LODNew);
219
220     wined3d_mutex_lock();
221     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
222     wined3d_mutex_unlock();
223
224     return ret;
225 }
226
227 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLOD(IDirect3DCubeTexture9 *iface)
228 {
229     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
230     DWORD ret;
231
232     TRACE("iface %p.\n", iface);
233
234     wined3d_mutex_lock();
235     ret = wined3d_texture_get_lod(This->wined3d_texture);
236     wined3d_mutex_unlock();
237
238     return ret;
239 }
240
241 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLevelCount(IDirect3DCubeTexture9 *iface)
242 {
243     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
244     DWORD ret;
245
246     TRACE("iface %p.\n", iface);
247
248     wined3d_mutex_lock();
249     ret = wined3d_texture_get_level_count(This->wined3d_texture);
250     wined3d_mutex_unlock();
251
252     return ret;
253 }
254
255 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetAutoGenFilterType(IDirect3DCubeTexture9 *iface,
256         D3DTEXTUREFILTERTYPE FilterType)
257 {
258     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
259     HRESULT hr;
260
261     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
262
263     wined3d_mutex_lock();
264     hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (WINED3DTEXTUREFILTERTYPE)FilterType);
265     wined3d_mutex_unlock();
266
267     return hr;
268 }
269
270 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DCubeTexture9Impl_GetAutoGenFilterType(IDirect3DCubeTexture9 *iface)
271 {
272     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
273     D3DTEXTUREFILTERTYPE ret;
274
275     TRACE("iface %p.\n", iface);
276
277     wined3d_mutex_lock();
278     ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
279     wined3d_mutex_unlock();
280
281     return ret;
282 }
283
284 static void WINAPI IDirect3DCubeTexture9Impl_GenerateMipSubLevels(IDirect3DCubeTexture9 *iface)
285 {
286     IDirect3DCubeTexture9Impl *This = impl_from_IDirect3DCubeTexture9(iface);
287
288     TRACE("iface %p.\n", iface);
289
290     wined3d_mutex_lock();
291     wined3d_texture_generate_mipmaps(This->wined3d_texture);
292     wined3d_mutex_unlock();
293 }
294
295 /* IDirect3DCubeTexture9 Interface follow: */
296 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetLevelDesc(IDirect3DCubeTexture9 *iface,
297         UINT level, D3DSURFACE_DESC *desc)
298 {
299     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
300     struct wined3d_resource *sub_resource;
301     HRESULT hr = D3D_OK;
302
303     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
304
305     wined3d_mutex_lock();
306     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
307         hr = D3DERR_INVALIDCALL;
308     else
309     {
310         struct wined3d_resource_desc wined3d_desc;
311
312         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
313         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
314         desc->Type = wined3d_desc.resource_type;
315         desc->Usage = wined3d_desc.usage;
316         desc->Pool = wined3d_desc.pool;
317         desc->MultiSampleType = wined3d_desc.multisample_type;
318         desc->MultiSampleQuality = wined3d_desc.multisample_quality;
319         desc->Width = wined3d_desc.width;
320         desc->Height = wined3d_desc.height;
321     }
322     wined3d_mutex_unlock();
323
324     return hr;
325 }
326
327 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
328         D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface9 **surface)
329 {
330     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
331     struct wined3d_resource *sub_resource;
332     UINT sub_resource_idx;
333
334     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);
335
336     wined3d_mutex_lock();
337     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
338     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
339     {
340         wined3d_mutex_unlock();
341         return D3DERR_INVALIDCALL;
342     }
343
344     *surface = wined3d_resource_get_parent(sub_resource);
345     IDirect3DSurface9_AddRef(*surface);
346     wined3d_mutex_unlock();
347
348     return D3D_OK;
349 }
350
351 static HRESULT WINAPI IDirect3DCubeTexture9Impl_LockRect(IDirect3DCubeTexture9 *iface,
352         D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect,
353         DWORD flags)
354 {
355     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
356     struct wined3d_resource *sub_resource;
357     UINT sub_resource_idx;
358     HRESULT hr;
359
360     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
361             iface, face, level, locked_rect, rect, flags);
362
363     wined3d_mutex_lock();
364     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
365     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
366         hr = D3DERR_INVALIDCALL;
367     else
368         hr = IDirect3DSurface9_LockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource),
369                 locked_rect, rect, flags);
370     wined3d_mutex_unlock();
371
372     return hr;
373 }
374
375 static HRESULT WINAPI IDirect3DCubeTexture9Impl_UnlockRect(IDirect3DCubeTexture9 *iface,
376         D3DCUBEMAP_FACES face, UINT level)
377 {
378     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
379     struct wined3d_resource *sub_resource;
380     UINT sub_resource_idx;
381     HRESULT hr;
382
383     TRACE("iface %p, face %#x, level %u.\n", iface, face, level);
384
385     wined3d_mutex_lock();
386     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
387     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
388         hr = D3DERR_INVALIDCALL;
389     else
390         hr = IDirect3DSurface9_UnlockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource));
391     wined3d_mutex_unlock();
392
393     return hr;
394 }
395
396 static HRESULT  WINAPI IDirect3DCubeTexture9Impl_AddDirtyRect(IDirect3DCubeTexture9 *iface,
397         D3DCUBEMAP_FACES face, const RECT *dirty_rect)
398 {
399     IDirect3DCubeTexture9Impl *texture = impl_from_IDirect3DCubeTexture9(iface);
400     HRESULT hr;
401
402     TRACE("iface %p, face %#x, dirty_rect %s.\n",
403             iface, face, wine_dbgstr_rect(dirty_rect));
404
405     wined3d_mutex_lock();
406     if (!dirty_rect)
407         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
408     else
409     {
410         WINED3DBOX dirty_region;
411
412         dirty_region.Left = dirty_rect->left;
413         dirty_region.Top = dirty_rect->top;
414         dirty_region.Right = dirty_rect->right;
415         dirty_region.Bottom = dirty_rect->bottom;
416         dirty_region.Front = 0;
417         dirty_region.Back = 1;
418         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, &dirty_region);
419     }
420     wined3d_mutex_unlock();
421
422     return hr;
423 }
424
425 static const IDirect3DCubeTexture9Vtbl Direct3DCubeTexture9_Vtbl =
426 {
427     /* IUnknown */
428     IDirect3DCubeTexture9Impl_QueryInterface,
429     IDirect3DCubeTexture9Impl_AddRef,
430     IDirect3DCubeTexture9Impl_Release,
431     /* IDirect3DResource9 */
432     IDirect3DCubeTexture9Impl_GetDevice,
433     IDirect3DCubeTexture9Impl_SetPrivateData,
434     IDirect3DCubeTexture9Impl_GetPrivateData,
435     IDirect3DCubeTexture9Impl_FreePrivateData,
436     IDirect3DCubeTexture9Impl_SetPriority,
437     IDirect3DCubeTexture9Impl_GetPriority,
438     IDirect3DCubeTexture9Impl_PreLoad,
439     IDirect3DCubeTexture9Impl_GetType,
440     /* IDirect3DBaseTexture9 */
441     IDirect3DCubeTexture9Impl_SetLOD,
442     IDirect3DCubeTexture9Impl_GetLOD,
443     IDirect3DCubeTexture9Impl_GetLevelCount,
444     IDirect3DCubeTexture9Impl_SetAutoGenFilterType,
445     IDirect3DCubeTexture9Impl_GetAutoGenFilterType,
446     IDirect3DCubeTexture9Impl_GenerateMipSubLevels,
447     IDirect3DCubeTexture9Impl_GetLevelDesc,
448     IDirect3DCubeTexture9Impl_GetCubeMapSurface,
449     IDirect3DCubeTexture9Impl_LockRect,
450     IDirect3DCubeTexture9Impl_UnlockRect,
451     IDirect3DCubeTexture9Impl_AddDirtyRect
452 };
453
454 static void STDMETHODCALLTYPE cubetexture_wined3d_object_destroyed(void *parent)
455 {
456     HeapFree(GetProcessHeap(), 0, parent);
457 }
458
459 static const struct wined3d_parent_ops d3d9_cubetexture_wined3d_parent_ops =
460 {
461     cubetexture_wined3d_object_destroyed,
462 };
463
464 HRESULT cubetexture_init(IDirect3DCubeTexture9Impl *texture, IDirect3DDevice9Impl *device,
465         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
466 {
467     HRESULT hr;
468
469     texture->IDirect3DCubeTexture9_iface.lpVtbl = &Direct3DCubeTexture9_Vtbl;
470     texture->ref = 1;
471
472     wined3d_mutex_lock();
473     hr = IWineD3DDevice_CreateCubeTexture(device->WineD3DDevice, edge_length,
474             levels, usage, wined3dformat_from_d3dformat(format), pool, texture,
475             &d3d9_cubetexture_wined3d_parent_ops, &texture->wined3d_texture);
476     wined3d_mutex_unlock();
477     if (FAILED(hr))
478     {
479         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
480         return hr;
481     }
482
483     texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
484     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
485
486     return D3D_OK;
487 }