msxml3: Release IDispatchEx dynamic data properly.
[wine] / dlls / d3d8 / cubetexture.c
1 /*
2  * IDirect3DCubeTexture8 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 IDirect3DCubeTexture8Impl *impl_from_IDirect3DCubeTexture8(IDirect3DCubeTexture8 *iface)
27 {
28     return CONTAINING_RECORD(iface, IDirect3DCubeTexture8Impl, IDirect3DCubeTexture8_iface);
29 }
30
31 static HRESULT WINAPI IDirect3DCubeTexture8Impl_QueryInterface(IDirect3DCubeTexture8 *iface,
32         REFIID riid, void **ppobj)
33 {
34     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(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_IDirect3DResource8)
40         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
41         || IsEqualGUID(riid, &IID_IDirect3DCubeTexture8)) {
42         IUnknown_AddRef(iface);
43         *ppobj = This;
44         return S_OK;
45     }
46
47     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
48     *ppobj = NULL;
49     return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI IDirect3DCubeTexture8Impl_AddRef(IDirect3DCubeTexture8 *iface)
53 {
54     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
55     ULONG ref = InterlockedIncrement(&This->ref);
56
57     TRACE("%p increasing refcount to %u.\n", iface, ref);
58
59     if (ref == 1)
60     {
61         IUnknown_AddRef(This->parentDevice);
62         wined3d_mutex_lock();
63         wined3d_texture_incref(This->wined3d_texture);
64         wined3d_mutex_unlock();
65     }
66
67     return ref;
68 }
69
70 static ULONG WINAPI IDirect3DCubeTexture8Impl_Release(IDirect3DCubeTexture8 *iface)
71 {
72     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
73     ULONG ref = InterlockedDecrement(&This->ref);
74
75     TRACE("%p decreasing refcount to %u.\n", iface, ref);
76
77     if (ref == 0) {
78         IDirect3DDevice8 *parentDevice = This->parentDevice;
79
80         TRACE("Releasing child %p.\n", This->wined3d_texture);
81
82         wined3d_mutex_lock();
83         wined3d_texture_decref(This->wined3d_texture);
84         wined3d_mutex_unlock();
85
86         /* Release the device last, as it may cause the device to be destroyed. */
87         IDirect3DDevice8_Release(parentDevice);
88     }
89     return ref;
90 }
91
92 /* IDirect3DCubeTexture8 IDirect3DResource8 Interface follow: */
93 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetDevice(IDirect3DCubeTexture8 *iface,
94         IDirect3DDevice8 **device)
95 {
96     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
97
98     TRACE("iface %p, device %p.\n", iface, device);
99
100     *device = This->parentDevice;
101     IDirect3DDevice8_AddRef(*device);
102
103     TRACE("Returning device %p.\n", *device);
104
105     return D3D_OK;
106 }
107
108 static HRESULT WINAPI IDirect3DCubeTexture8Impl_SetPrivateData(IDirect3DCubeTexture8 *iface,
109         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
110 {
111     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
112     struct wined3d_resource *resource;
113     HRESULT hr;
114
115     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
116             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
117
118     wined3d_mutex_lock();
119     resource = wined3d_texture_get_resource(This->wined3d_texture);
120     hr = wined3d_resource_set_private_data(resource, refguid, pData, SizeOfData, Flags);
121     wined3d_mutex_unlock();
122
123     return hr;
124 }
125
126 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetPrivateData(IDirect3DCubeTexture8 *iface,
127         REFGUID refguid, void *pData, DWORD *pSizeOfData)
128 {
129     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
130     struct wined3d_resource *resource;
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     resource = wined3d_texture_get_resource(This->wined3d_texture);
138     hr = wined3d_resource_get_private_data(resource, refguid, pData, pSizeOfData);
139     wined3d_mutex_unlock();
140
141     return hr;
142 }
143
144 static HRESULT WINAPI IDirect3DCubeTexture8Impl_FreePrivateData(IDirect3DCubeTexture8 *iface,
145         REFGUID refguid)
146 {
147     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
148     struct wined3d_resource *resource;
149     HRESULT hr;
150
151     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
152
153     wined3d_mutex_lock();
154     resource = wined3d_texture_get_resource(This->wined3d_texture);
155     hr = wined3d_resource_free_private_data(resource, refguid);
156     wined3d_mutex_unlock();
157
158     return hr;
159 }
160
161 static DWORD WINAPI IDirect3DCubeTexture8Impl_SetPriority(IDirect3DCubeTexture8 *iface,
162         DWORD PriorityNew)
163 {
164     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
165     DWORD ret;
166
167     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
168
169     wined3d_mutex_lock();
170     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
171     wined3d_mutex_unlock();
172
173     return ret;
174 }
175
176 static DWORD WINAPI IDirect3DCubeTexture8Impl_GetPriority(IDirect3DCubeTexture8 *iface)
177 {
178     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
179     DWORD ret;
180
181     TRACE("iface %p.\n", iface);
182
183     wined3d_mutex_lock();
184     ret =  wined3d_texture_get_priority(This->wined3d_texture);
185     wined3d_mutex_unlock();
186
187     return ret;
188 }
189
190 static void WINAPI IDirect3DCubeTexture8Impl_PreLoad(IDirect3DCubeTexture8 *iface)
191 {
192     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
193
194     TRACE("iface %p.\n", iface);
195
196     wined3d_mutex_lock();
197     wined3d_texture_preload(This->wined3d_texture);
198     wined3d_mutex_unlock();
199 }
200
201 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture8Impl_GetType(IDirect3DCubeTexture8 *iface)
202 {
203     TRACE("iface %p.\n", iface);
204
205     return D3DRTYPE_CUBETEXTURE;
206 }
207
208 /* IDirect3DCubeTexture8 IDirect3DBaseTexture8 Interface follow: */
209 static DWORD WINAPI IDirect3DCubeTexture8Impl_SetLOD(IDirect3DCubeTexture8 *iface, DWORD LODNew)
210 {
211     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
212     DWORD lod;
213
214     TRACE("iface %p, lod %u.\n", iface, LODNew);
215
216     wined3d_mutex_lock();
217     lod = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
218     wined3d_mutex_unlock();
219
220     return lod;
221 }
222
223 static DWORD WINAPI IDirect3DCubeTexture8Impl_GetLOD(IDirect3DCubeTexture8 *iface)
224 {
225     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
226     DWORD lod;
227
228     TRACE("iface %p.\n", iface);
229
230     wined3d_mutex_lock();
231     lod = wined3d_texture_get_lod(This->wined3d_texture);
232     wined3d_mutex_unlock();
233
234     return lod;
235 }
236
237 static DWORD WINAPI IDirect3DCubeTexture8Impl_GetLevelCount(IDirect3DCubeTexture8 *iface)
238 {
239     IDirect3DCubeTexture8Impl *This = impl_from_IDirect3DCubeTexture8(iface);
240     DWORD cnt;
241
242     TRACE("iface %p.\n", iface);
243
244     wined3d_mutex_lock();
245     cnt = wined3d_texture_get_level_count(This->wined3d_texture);
246     wined3d_mutex_unlock();
247
248     return cnt;
249 }
250
251 /* IDirect3DCubeTexture8 Interface follow: */
252 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetLevelDesc(IDirect3DCubeTexture8 *iface,
253         UINT level, D3DSURFACE_DESC *desc)
254 {
255     IDirect3DCubeTexture8Impl *texture = impl_from_IDirect3DCubeTexture8(iface);
256     struct wined3d_resource *sub_resource;
257     HRESULT hr = D3D_OK;
258
259     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
260
261     wined3d_mutex_lock();
262     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
263         hr = D3DERR_INVALIDCALL;
264     else
265     {
266         struct wined3d_resource_desc wined3d_desc;
267
268         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
269         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
270         desc->Type = wined3d_desc.resource_type;
271         desc->Usage = wined3d_desc.usage;
272         desc->Pool = wined3d_desc.pool;
273         desc->Size = wined3d_desc.size;
274         desc->MultiSampleType = wined3d_desc.multisample_type;
275         desc->Width = wined3d_desc.width;
276         desc->Height = wined3d_desc.height;
277     }
278     wined3d_mutex_unlock();
279
280     return hr;
281 }
282
283 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetCubeMapSurface(IDirect3DCubeTexture8 *iface,
284         D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface8 **surface)
285 {
286     IDirect3DCubeTexture8Impl *texture = impl_from_IDirect3DCubeTexture8(iface);
287     struct wined3d_resource *sub_resource;
288     UINT sub_resource_idx;
289
290     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);
291
292     wined3d_mutex_lock();
293     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
294     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
295     {
296         wined3d_mutex_unlock();
297         return D3DERR_INVALIDCALL;
298     }
299
300     *surface = wined3d_resource_get_parent(sub_resource);
301     IDirect3DSurface8_AddRef(*surface);
302     wined3d_mutex_unlock();
303
304     return D3D_OK;
305 }
306
307 static HRESULT WINAPI IDirect3DCubeTexture8Impl_LockRect(IDirect3DCubeTexture8 *iface,
308         D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect,
309         DWORD flags)
310 {
311     IDirect3DCubeTexture8Impl *texture = impl_from_IDirect3DCubeTexture8(iface);
312     struct wined3d_resource *sub_resource;
313     UINT sub_resource_idx;
314     HRESULT hr;
315
316     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
317             iface, face, level, locked_rect, rect, flags);
318
319     wined3d_mutex_lock();
320     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
321     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
322         hr = D3DERR_INVALIDCALL;
323     else
324         hr = IDirect3DSurface8_LockRect((IDirect3DSurface8 *)wined3d_resource_get_parent(sub_resource),
325                 locked_rect, rect, flags);
326     wined3d_mutex_unlock();
327
328     return hr;
329 }
330
331 static HRESULT WINAPI IDirect3DCubeTexture8Impl_UnlockRect(IDirect3DCubeTexture8 *iface,
332         D3DCUBEMAP_FACES face, UINT level)
333 {
334     IDirect3DCubeTexture8Impl *texture = impl_from_IDirect3DCubeTexture8(iface);
335     struct wined3d_resource *sub_resource;
336     UINT sub_resource_idx;
337     HRESULT hr;
338
339     TRACE("iface %p, face %#x, level %u.\n", iface, face, level);
340
341     wined3d_mutex_lock();
342     sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
343     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
344         hr = D3DERR_INVALIDCALL;
345     else
346         hr = IDirect3DSurface8_UnlockRect((IDirect3DSurface8 *)wined3d_resource_get_parent(sub_resource));
347     wined3d_mutex_unlock();
348
349     return hr;
350 }
351
352 static HRESULT WINAPI IDirect3DCubeTexture8Impl_AddDirtyRect(IDirect3DCubeTexture8 *iface,
353         D3DCUBEMAP_FACES face, const RECT *dirty_rect)
354 {
355     IDirect3DCubeTexture8Impl *texture = impl_from_IDirect3DCubeTexture8(iface);
356     HRESULT hr;
357
358     TRACE("iface %p, face %#x, dirty_rect %s.\n",
359             iface, face, wine_dbgstr_rect(dirty_rect));
360
361     wined3d_mutex_lock();
362     if (!dirty_rect)
363         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
364     else
365     {
366         WINED3DBOX dirty_region;
367
368         dirty_region.Left = dirty_rect->left;
369         dirty_region.Top = dirty_rect->top;
370         dirty_region.Right = dirty_rect->right;
371         dirty_region.Bottom = dirty_rect->bottom;
372         dirty_region.Front = 0;
373         dirty_region.Back = 1;
374         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, &dirty_region);
375     }
376     wined3d_mutex_unlock();
377
378     return hr;
379 }
380
381 static const IDirect3DCubeTexture8Vtbl Direct3DCubeTexture8_Vtbl =
382 {
383     /* IUnknown */
384     IDirect3DCubeTexture8Impl_QueryInterface,
385     IDirect3DCubeTexture8Impl_AddRef,
386     IDirect3DCubeTexture8Impl_Release,
387     /* IDirect3DResource8 */
388     IDirect3DCubeTexture8Impl_GetDevice,
389     IDirect3DCubeTexture8Impl_SetPrivateData,
390     IDirect3DCubeTexture8Impl_GetPrivateData,
391     IDirect3DCubeTexture8Impl_FreePrivateData,
392     IDirect3DCubeTexture8Impl_SetPriority,
393     IDirect3DCubeTexture8Impl_GetPriority,
394     IDirect3DCubeTexture8Impl_PreLoad,
395     IDirect3DCubeTexture8Impl_GetType,
396     /* IDirect3DBaseTexture8 */
397     IDirect3DCubeTexture8Impl_SetLOD,
398     IDirect3DCubeTexture8Impl_GetLOD,
399     IDirect3DCubeTexture8Impl_GetLevelCount,
400     /* IDirect3DCubeTexture8 */
401     IDirect3DCubeTexture8Impl_GetLevelDesc,
402     IDirect3DCubeTexture8Impl_GetCubeMapSurface,
403     IDirect3DCubeTexture8Impl_LockRect,
404     IDirect3DCubeTexture8Impl_UnlockRect,
405     IDirect3DCubeTexture8Impl_AddDirtyRect
406 };
407
408 static void STDMETHODCALLTYPE d3d8_cubetexture_wined3d_object_destroyed(void *parent)
409 {
410     HeapFree(GetProcessHeap(), 0, parent);
411 }
412
413 static const struct wined3d_parent_ops d3d8_cubetexture_wined3d_parent_ops =
414 {
415     d3d8_cubetexture_wined3d_object_destroyed,
416 };
417
418 HRESULT cubetexture_init(IDirect3DCubeTexture8Impl *texture, IDirect3DDevice8Impl *device,
419         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
420 {
421     HRESULT hr;
422
423     texture->IDirect3DCubeTexture8_iface.lpVtbl = &Direct3DCubeTexture8_Vtbl;
424     texture->ref = 1;
425
426     wined3d_mutex_lock();
427     hr = wined3d_texture_create_cube(device->wined3d_device, edge_length, levels,
428             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool, texture,
429             &d3d8_cubetexture_wined3d_parent_ops, &texture->wined3d_texture);
430     wined3d_mutex_unlock();
431     if (FAILED(hr))
432     {
433         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
434         return hr;
435     }
436
437     texture->parentDevice = &device->IDirect3DDevice8_iface;
438     IDirect3DDevice8_AddRef(texture->parentDevice);
439
440     return D3D_OK;
441 }