wined3d: Don't require wined3d object parents to be COM objects.
[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         IWineD3DCubeTexture_AddRef(This->wineD3DCubeTexture);
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->wineD3DCubeTexture);
76
77         wined3d_mutex_lock();
78         IWineD3DCubeTexture_Release(This->wineD3DCubeTexture);
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 = IWineD3DCubeTexture_SetPrivateData(This->wineD3DCubeTexture,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 = IWineD3DCubeTexture_GetPrivateData(This->wineD3DCubeTexture,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 = IWineD3DCubeTexture_FreePrivateData(This->wineD3DCubeTexture,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 = IWineD3DCubeTexture_SetPriority(This->wineD3DCubeTexture, 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 = IWineD3DCubeTexture_GetPriority(This->wineD3DCubeTexture);
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     IWineD3DCubeTexture_PreLoad(This->wineD3DCubeTexture);
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 = IWineD3DCubeTexture_GetType(This->wineD3DCubeTexture);
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 = IWineD3DCubeTexture_SetLOD(This->wineD3DCubeTexture, 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 = IWineD3DCubeTexture_GetLOD(This->wineD3DCubeTexture);
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 = IWineD3DCubeTexture_GetLevelCount(This->wineD3DCubeTexture);
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 = IWineD3DCubeTexture_SetAutoGenFilterType(This->wineD3DCubeTexture, (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) IWineD3DCubeTexture_GetAutoGenFilterType(This->wineD3DCubeTexture);
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     IWineD3DCubeTexture_GenerateMipSubLevels(This->wineD3DCubeTexture);
265     wined3d_mutex_unlock();
266 }
267
268 /* IDirect3DCubeTexture9 Interface follow: */
269 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetLevelDesc(LPDIRECT3DCUBETEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
270     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
271     WINED3DSURFACE_DESC wined3ddesc;
272     HRESULT hr;
273
274     TRACE("iface %p, level %u, desc %p.\n", iface, Level, pDesc);
275
276     wined3d_mutex_lock();
277     hr = IWineD3DCubeTexture_GetLevelDesc(This->wineD3DCubeTexture, Level, &wined3ddesc);
278     wined3d_mutex_unlock();
279
280     if (SUCCEEDED(hr))
281     {
282         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
283         pDesc->Type = wined3ddesc.resource_type;
284         pDesc->Usage = wined3ddesc.usage;
285         pDesc->Pool = wined3ddesc.pool;
286         pDesc->MultiSampleType = wined3ddesc.multisample_type;
287         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
288         pDesc->Width = wined3ddesc.width;
289         pDesc->Height = wined3ddesc.height;
290     }
291
292     return hr;
293 }
294
295 static HRESULT WINAPI IDirect3DCubeTexture9Impl_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
296         D3DCUBEMAP_FACES FaceType, UINT Level, IDirect3DSurface9 **ppCubeMapSurface)
297 {
298     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
299     IWineD3DSurface *mySurface = NULL;
300     HRESULT hr;
301
302     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, FaceType, Level, ppCubeMapSurface);
303
304     wined3d_mutex_lock();
305     hr = IWineD3DCubeTexture_GetCubeMapSurface(This->wineD3DCubeTexture,
306             (WINED3DCUBEMAP_FACES)FaceType, Level, &mySurface);
307     if (SUCCEEDED(hr) && ppCubeMapSurface)
308     {
309         *ppCubeMapSurface = IWineD3DCubeTexture_GetParent(mySurface);
310         IDirect3DSurface9_AddRef(*ppCubeMapSurface);
311         IWineD3DCubeTexture_Release(mySurface);
312     }
313     wined3d_mutex_unlock();
314
315     return hr;
316 }
317
318 static HRESULT WINAPI IDirect3DCubeTexture9Impl_LockRect(LPDIRECT3DCUBETEXTURE9 iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
319     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
320     HRESULT hr;
321
322     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
323             iface, FaceType, Level, pLockedRect, pRect, Flags);
324
325     wined3d_mutex_lock();
326     hr = IWineD3DCubeTexture_LockRect(This->wineD3DCubeTexture, (WINED3DCUBEMAP_FACES) FaceType, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
327     wined3d_mutex_unlock();
328
329     return hr;
330 }
331
332 static HRESULT WINAPI IDirect3DCubeTexture9Impl_UnlockRect(LPDIRECT3DCUBETEXTURE9 iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
333     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
334     HRESULT hr;
335
336     TRACE("iface %p, face %#x, level %u.\n", iface, FaceType, Level);
337
338     wined3d_mutex_lock();
339     hr = IWineD3DCubeTexture_UnlockRect(This->wineD3DCubeTexture, (WINED3DCUBEMAP_FACES) FaceType, Level);
340     wined3d_mutex_unlock();
341
342     return hr;
343 }
344
345 static HRESULT  WINAPI IDirect3DCubeTexture9Impl_AddDirtyRect(LPDIRECT3DCUBETEXTURE9 iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
346     IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl *)iface;
347     HRESULT hr;
348
349     TRACE("iface %p, face %#x, dirty_rect %p.\n", iface, FaceType, pDirtyRect);
350
351     wined3d_mutex_lock();
352     hr = IWineD3DCubeTexture_AddDirtyRect(This->wineD3DCubeTexture, (WINED3DCUBEMAP_FACES) FaceType, pDirtyRect);
353     wined3d_mutex_unlock();
354
355     return hr;
356 }
357
358
359 static const IDirect3DCubeTexture9Vtbl Direct3DCubeTexture9_Vtbl =
360 {
361     /* IUnknown */
362     IDirect3DCubeTexture9Impl_QueryInterface,
363     IDirect3DCubeTexture9Impl_AddRef,
364     IDirect3DCubeTexture9Impl_Release,
365     /* IDirect3DResource9 */
366     IDirect3DCubeTexture9Impl_GetDevice,
367     IDirect3DCubeTexture9Impl_SetPrivateData,
368     IDirect3DCubeTexture9Impl_GetPrivateData,
369     IDirect3DCubeTexture9Impl_FreePrivateData,
370     IDirect3DCubeTexture9Impl_SetPriority,
371     IDirect3DCubeTexture9Impl_GetPriority,
372     IDirect3DCubeTexture9Impl_PreLoad,
373     IDirect3DCubeTexture9Impl_GetType,
374     /* IDirect3DBaseTexture9 */
375     IDirect3DCubeTexture9Impl_SetLOD,
376     IDirect3DCubeTexture9Impl_GetLOD,
377     IDirect3DCubeTexture9Impl_GetLevelCount,
378     IDirect3DCubeTexture9Impl_SetAutoGenFilterType,
379     IDirect3DCubeTexture9Impl_GetAutoGenFilterType,
380     IDirect3DCubeTexture9Impl_GenerateMipSubLevels,
381     IDirect3DCubeTexture9Impl_GetLevelDesc,
382     IDirect3DCubeTexture9Impl_GetCubeMapSurface,
383     IDirect3DCubeTexture9Impl_LockRect,
384     IDirect3DCubeTexture9Impl_UnlockRect,
385     IDirect3DCubeTexture9Impl_AddDirtyRect
386 };
387
388 static void STDMETHODCALLTYPE cubetexture_wined3d_object_destroyed(void *parent)
389 {
390     HeapFree(GetProcessHeap(), 0, parent);
391 }
392
393 static const struct wined3d_parent_ops d3d9_cubetexture_wined3d_parent_ops =
394 {
395     cubetexture_wined3d_object_destroyed,
396 };
397
398 HRESULT cubetexture_init(IDirect3DCubeTexture9Impl *texture, IDirect3DDevice9Impl *device,
399         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
400 {
401     HRESULT hr;
402
403     texture->lpVtbl = &Direct3DCubeTexture9_Vtbl;
404     texture->ref = 1;
405
406     wined3d_mutex_lock();
407     hr = IWineD3DDevice_CreateCubeTexture(device->WineD3DDevice, edge_length,
408             levels, usage, wined3dformat_from_d3dformat(format), pool, texture,
409             &d3d9_cubetexture_wined3d_parent_ops, &texture->wineD3DCubeTexture);
410     wined3d_mutex_unlock();
411     if (FAILED(hr))
412     {
413         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
414         return hr;
415     }
416
417     texture->parentDevice = (IDirect3DDevice9Ex *)device;
418     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
419
420     return D3D_OK;
421 }