comdlg32: Add IServiceProvider implementation to the Item Dialog.
[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 /* IDirect3DCubeTexture9 IUnknown parts follow: */
30 static HRESULT WINAPI IDirect3DCubeTexture9Impl_QueryInterface(LPDIRECT3DCUBETEXTURE9 iface, REFIID riid, LPVOID* ppobj) {
31     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
32
33     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
34
35     if (IsEqualGUID(riid, &IID_IUnknown)
36         || IsEqualGUID(riid, &IID_IDirect3DResource9)
37         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
38         || IsEqualGUID(riid, &IID_IDirect3DCubeTexture9)) {
39         IDirect3DCubeTexture9_AddRef(iface);
40         *ppobj = This;
41         return S_OK;
42     }
43
44     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
45     *ppobj = NULL;
46     return E_NOINTERFACE;
47 }
48
49 static ULONG WINAPI IDirect3DCubeTexture9Impl_AddRef(LPDIRECT3DCUBETEXTURE9 iface) {
50     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
51     ULONG ref = InterlockedIncrement(&This->ref);
52
53     TRACE("%p increasing refcount to %u.\n", iface, ref);
54
55     if (ref == 1)
56     {
57         IDirect3DDevice9Ex_AddRef(This->parentDevice);
58         wined3d_mutex_lock();
59         wined3d_texture_incref(This->wined3d_texture);
60         wined3d_mutex_unlock();
61     }
62
63     return ref;
64 }
65
66 static ULONG WINAPI IDirect3DCubeTexture9Impl_Release(LPDIRECT3DCUBETEXTURE9 iface) {
67     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
68     ULONG ref = InterlockedDecrement(&This->ref);
69
70     TRACE("%p decreasing refcount to %u.\n", iface, ref);
71
72     if (ref == 0) {
73         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
74
75         TRACE("Releasing child %p.\n", This->wined3d_texture);
76
77         wined3d_mutex_lock();
78         wined3d_texture_decref(This->wined3d_texture);
79         wined3d_mutex_unlock();
80
81         /* Release the device last, as it may cause the device to be destroyed. */
82         IDirect3DDevice9Ex_Release(parentDevice);
83     }
84     return ref;
85 }
86
87 /* IDirect3DCubeTexture9 IDirect3DResource9 Interface follow: */
88 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetDevice(IDirect3DCubeTexture9 *iface, IDirect3DDevice9 **device)
89 {
90     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
91
92     TRACE("iface %p, device %p.\n", iface, device);
93
94     *device = (IDirect3DDevice9 *)This->parentDevice;
95     IDirect3DDevice9_AddRef(*device);
96
97     TRACE("Returning device %p.\n", *device);
98
99     return D3D_OK;
100 }
101
102 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetPrivateData(LPDIRECT3DCUBETEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
103     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
104     HRESULT hr;
105
106     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
107             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
108
109     wined3d_mutex_lock();
110     hr = wined3d_texture_set_private_data(This->wined3d_texture, refguid, pData, SizeOfData, Flags);
111     wined3d_mutex_unlock();
112
113     return hr;
114 }
115
116 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetPrivateData(LPDIRECT3DCUBETEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
117     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
118     HRESULT hr;
119
120     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
121             iface, debugstr_guid(refguid), pData, pSizeOfData);
122
123     wined3d_mutex_lock();
124     hr = wined3d_texture_get_private_data(This->wined3d_texture, refguid, pData, pSizeOfData);
125     wined3d_mutex_unlock();
126
127     return hr;
128 }
129
130 static HRESULT WINAPI IDirect3DCubeTexture9Impl_FreePrivateData(LPDIRECT3DCUBETEXTURE9 iface, REFGUID refguid) {
131     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
132     HRESULT hr;
133
134     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
135
136     wined3d_mutex_lock();
137     hr = wined3d_texture_free_private_data(This->wined3d_texture, refguid);
138     wined3d_mutex_unlock();
139
140     return hr;
141 }
142
143 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetPriority(LPDIRECT3DCUBETEXTURE9 iface, DWORD PriorityNew) {
144     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
145     DWORD ret;
146
147     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
148
149     wined3d_mutex_lock();
150     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
151     wined3d_mutex_unlock();
152
153     return ret;
154 }
155
156 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetPriority(LPDIRECT3DCUBETEXTURE9 iface) {
157     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
158     DWORD ret;
159
160     TRACE("iface %p.\n", iface);
161
162     wined3d_mutex_lock();
163     ret = wined3d_texture_get_priority(This->wined3d_texture);
164     wined3d_mutex_unlock();
165
166     return ret;
167 }
168
169 static void WINAPI IDirect3DCubeTexture9Impl_PreLoad(LPDIRECT3DCUBETEXTURE9 iface) {
170     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
171
172     TRACE("iface %p.\n", iface);
173
174     wined3d_mutex_lock();
175     wined3d_texture_preload(This->wined3d_texture);
176     wined3d_mutex_unlock();
177 }
178
179 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture9Impl_GetType(LPDIRECT3DCUBETEXTURE9 iface) {
180     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
181     D3DRESOURCETYPE ret;
182
183     TRACE("iface %p.\n", iface);
184
185     wined3d_mutex_lock();
186     ret = wined3d_texture_get_type(This->wined3d_texture);
187     wined3d_mutex_unlock();
188
189     return ret;
190 }
191
192 /* IDirect3DCubeTexture9 IDirect3DBaseTexture9 Interface follow: */
193 static DWORD WINAPI IDirect3DCubeTexture9Impl_SetLOD(LPDIRECT3DCUBETEXTURE9 iface, DWORD LODNew) {
194     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
195     DWORD ret;
196
197     TRACE("iface %p, lod %u.\n", iface, LODNew);
198
199     wined3d_mutex_lock();
200     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
201     wined3d_mutex_unlock();
202
203     return ret;
204 }
205
206 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLOD(LPDIRECT3DCUBETEXTURE9 iface) {
207     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
208     DWORD ret;
209
210     TRACE("iface %p.\n", iface);
211
212     wined3d_mutex_lock();
213     ret = wined3d_texture_get_lod(This->wined3d_texture);
214     wined3d_mutex_unlock();
215
216     return ret;
217 }
218
219 static DWORD WINAPI IDirect3DCubeTexture9Impl_GetLevelCount(LPDIRECT3DCUBETEXTURE9 iface) {
220     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
221     DWORD ret;
222
223     TRACE("iface %p.\n", iface);
224
225     wined3d_mutex_lock();
226     ret = wined3d_texture_get_level_count(This->wined3d_texture);
227     wined3d_mutex_unlock();
228
229     return ret;
230 }
231
232 static HRESULT WINAPI IDirect3DCubeTexture9Impl_SetAutoGenFilterType(LPDIRECT3DCUBETEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
233     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
234     HRESULT hr;
235
236     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
237
238     wined3d_mutex_lock();
239     hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (WINED3DTEXTUREFILTERTYPE)FilterType);
240     wined3d_mutex_unlock();
241
242     return hr;
243 }
244
245 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DCubeTexture9Impl_GetAutoGenFilterType(LPDIRECT3DCUBETEXTURE9 iface) {
246     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
247     D3DTEXTUREFILTERTYPE ret;
248
249     TRACE("iface %p.\n", iface);
250
251     wined3d_mutex_lock();
252     ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
253     wined3d_mutex_unlock();
254
255     return ret;
256 }
257
258 static void WINAPI IDirect3DCubeTexture9Impl_GenerateMipSubLevels(LPDIRECT3DCUBETEXTURE9 iface) {
259     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
260
261     TRACE("iface %p.\n", iface);
262
263     wined3d_mutex_lock();
264     wined3d_texture_generate_mipmaps(This->wined3d_texture);
265     wined3d_mutex_unlock();
266 }
267
268 /* IDirect3DCubeTexture9 Interface follow: */
269 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetLevelDesc(IDirect3DCubeTexture9 *iface,
270         UINT level, D3DSURFACE_DESC *desc)
271 {
272     IDirect3DCubeTexture9Impl *texture = (IDirect3DCubeTexture9Impl *)iface;
273     struct wined3d_resource *sub_resource;
274     HRESULT hr = D3D_OK;
275
276     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
277
278     wined3d_mutex_lock();
279     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
280         hr = D3DERR_INVALIDCALL;
281     else
282     {
283         struct wined3d_resource_desc wined3d_desc;
284
285         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
286         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
287         desc->Type = wined3d_desc.resource_type;
288         desc->Usage = wined3d_desc.usage;
289         desc->Pool = wined3d_desc.pool;
290         desc->MultiSampleType = wined3d_desc.multisample_type;
291         desc->MultiSampleQuality = wined3d_desc.multisample_quality;
292         desc->Width = wined3d_desc.width;
293         desc->Height = wined3d_desc.height;
294     }
295     wined3d_mutex_unlock();
296
297     return hr;
298 }
299
300 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
301         D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface9 **surface)
302 {
303     IDirect3DCubeTexture9Impl *texture = (IDirect3DCubeTexture9Impl *)iface;
304     struct wined3d_resource *sub_resource;
305     UINT sub_resource_idx;
306
307     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);
308
309     wined3d_mutex_lock();
310     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
311     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
312     {
313         wined3d_mutex_unlock();
314         return D3DERR_INVALIDCALL;
315     }
316
317     *surface = wined3d_resource_get_parent(sub_resource);
318     IDirect3DSurface9_AddRef(*surface);
319     wined3d_mutex_unlock();
320
321     return D3D_OK;
322 }
323
324 static HRESULT WINAPI IDirect3DCubeTexture9Impl_LockRect(IDirect3DCubeTexture9 *iface,
325         D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
326 {
327     IDirect3DCubeTexture9Impl *texture = (IDirect3DCubeTexture9Impl *)iface;
328     struct wined3d_resource *sub_resource;
329     UINT sub_resource_idx;
330     HRESULT hr;
331
332     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
333             iface, face, level, locked_rect, rect, flags);
334
335     wined3d_mutex_lock();
336     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
337     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
338         hr = D3DERR_INVALIDCALL;
339     else
340         hr = IDirect3DSurface9_LockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource),
341                 locked_rect, rect, flags);
342     wined3d_mutex_unlock();
343
344     return hr;
345 }
346
347 static HRESULT WINAPI IDirect3DCubeTexture9Impl_UnlockRect(IDirect3DCubeTexture9 *iface,
348         D3DCUBEMAP_FACES face, UINT level)
349 {
350     IDirect3DCubeTexture9Impl *texture = (IDirect3DCubeTexture9Impl *)iface;
351     struct wined3d_resource *sub_resource;
352     UINT sub_resource_idx;
353     HRESULT hr;
354
355     TRACE("iface %p, face %#x, level %u.\n", iface, face, level);
356
357     wined3d_mutex_lock();
358     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
359     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
360         hr = D3DERR_INVALIDCALL;
361     else
362         hr = IDirect3DSurface9_UnlockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource));
363     wined3d_mutex_unlock();
364
365     return hr;
366 }
367
368 static HRESULT  WINAPI IDirect3DCubeTexture9Impl_AddDirtyRect(IDirect3DCubeTexture9 *iface,
369         D3DCUBEMAP_FACES face, const RECT *dirty_rect)
370 {
371     IDirect3DCubeTexture9Impl *texture = (IDirect3DCubeTexture9Impl *)iface;
372     HRESULT hr;
373
374     TRACE("iface %p, face %#x, dirty_rect %s.\n",
375             iface, face, wine_dbgstr_rect(dirty_rect));
376
377     wined3d_mutex_lock();
378     if (!dirty_rect)
379         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
380     else
381     {
382         WINED3DBOX dirty_region;
383
384         dirty_region.Left = dirty_rect->left;
385         dirty_region.Top = dirty_rect->top;
386         dirty_region.Right = dirty_rect->right;
387         dirty_region.Bottom = dirty_rect->bottom;
388         dirty_region.Front = 0;
389         dirty_region.Back = 1;
390         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, &dirty_region);
391     }
392     wined3d_mutex_unlock();
393
394     return hr;
395 }
396
397 static const IDirect3DCubeTexture9Vtbl Direct3DCubeTexture9_Vtbl =
398 {
399     /* IUnknown */
400     IDirect3DCubeTexture9Impl_QueryInterface,
401     IDirect3DCubeTexture9Impl_AddRef,
402     IDirect3DCubeTexture9Impl_Release,
403     /* IDirect3DResource9 */
404     IDirect3DCubeTexture9Impl_GetDevice,
405     IDirect3DCubeTexture9Impl_SetPrivateData,
406     IDirect3DCubeTexture9Impl_GetPrivateData,
407     IDirect3DCubeTexture9Impl_FreePrivateData,
408     IDirect3DCubeTexture9Impl_SetPriority,
409     IDirect3DCubeTexture9Impl_GetPriority,
410     IDirect3DCubeTexture9Impl_PreLoad,
411     IDirect3DCubeTexture9Impl_GetType,
412     /* IDirect3DBaseTexture9 */
413     IDirect3DCubeTexture9Impl_SetLOD,
414     IDirect3DCubeTexture9Impl_GetLOD,
415     IDirect3DCubeTexture9Impl_GetLevelCount,
416     IDirect3DCubeTexture9Impl_SetAutoGenFilterType,
417     IDirect3DCubeTexture9Impl_GetAutoGenFilterType,
418     IDirect3DCubeTexture9Impl_GenerateMipSubLevels,
419     IDirect3DCubeTexture9Impl_GetLevelDesc,
420     IDirect3DCubeTexture9Impl_GetCubeMapSurface,
421     IDirect3DCubeTexture9Impl_LockRect,
422     IDirect3DCubeTexture9Impl_UnlockRect,
423     IDirect3DCubeTexture9Impl_AddDirtyRect
424 };
425
426 static void STDMETHODCALLTYPE cubetexture_wined3d_object_destroyed(void *parent)
427 {
428     HeapFree(GetProcessHeap(), 0, parent);
429 }
430
431 static const struct wined3d_parent_ops d3d9_cubetexture_wined3d_parent_ops =
432 {
433     cubetexture_wined3d_object_destroyed,
434 };
435
436 HRESULT cubetexture_init(IDirect3DCubeTexture9Impl *texture, IDirect3DDevice9Impl *device,
437         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
438 {
439     HRESULT hr;
440
441     texture->lpVtbl = &Direct3DCubeTexture9_Vtbl;
442     texture->ref = 1;
443
444     wined3d_mutex_lock();
445     hr = IWineD3DDevice_CreateCubeTexture(device->WineD3DDevice, edge_length,
446             levels, usage, wined3dformat_from_d3dformat(format), pool, texture,
447             &d3d9_cubetexture_wined3d_parent_ops, &texture->wined3d_texture);
448     wined3d_mutex_unlock();
449     if (FAILED(hr))
450     {
451         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
452         return hr;
453     }
454
455     texture->parentDevice = (IDirect3DDevice9Ex *)device;
456     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
457
458     return D3D_OK;
459 }