wined3d: Don't require wined3d object parents to be COM objects.
[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 /* IDirect3DCubeTexture8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DCubeTexture8Impl_QueryInterface(LPDIRECT3DCUBETEXTURE8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
29
30     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource8)
34         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
35         || IsEqualGUID(riid, &IID_IDirect3DCubeTexture8)) {
36         IUnknown_AddRef(iface);
37         *ppobj = This;
38         return S_OK;
39     }
40
41     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
42     *ppobj = NULL;
43     return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DCubeTexture8Impl_AddRef(LPDIRECT3DCUBETEXTURE8 iface) {
47     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
48     ULONG ref = InterlockedIncrement(&This->ref);
49
50     TRACE("%p increasing refcount to %u.\n", iface, ref);
51
52     if (ref == 1)
53     {
54         IUnknown_AddRef(This->parentDevice);
55         wined3d_mutex_lock();
56         IWineD3DCubeTexture_AddRef(This->wineD3DCubeTexture);
57         wined3d_mutex_unlock();
58     }
59
60     return ref;
61 }
62
63 static ULONG WINAPI IDirect3DCubeTexture8Impl_Release(LPDIRECT3DCUBETEXTURE8 iface) {
64     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
65     ULONG ref = InterlockedDecrement(&This->ref);
66
67     TRACE("%p decreasing refcount to %u.\n", iface, ref);
68
69     if (ref == 0) {
70         IDirect3DDevice8 *parentDevice = This->parentDevice;
71
72         TRACE("Releasing child %p\n", This->wineD3DCubeTexture);
73
74         wined3d_mutex_lock();
75         IWineD3DCubeTexture_Release(This->wineD3DCubeTexture);
76         wined3d_mutex_unlock();
77
78         /* Release the device last, as it may cause the device to be destroyed. */
79         IDirect3DDevice8_Release(parentDevice);
80     }
81     return ref;
82 }
83
84 /* IDirect3DCubeTexture8 IDirect3DResource8 Interface follow: */
85 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetDevice(IDirect3DCubeTexture8 *iface, IDirect3DDevice8 **device)
86 {
87     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
88
89     TRACE("iface %p, device %p.\n", iface, device);
90
91     *device = (IDirect3DDevice8 *)This->parentDevice;
92     IDirect3DDevice8_AddRef(*device);
93
94     TRACE("Returning device %p.\n", *device);
95
96     return D3D_OK;
97 }
98
99 static HRESULT WINAPI IDirect3DCubeTexture8Impl_SetPrivateData(LPDIRECT3DCUBETEXTURE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
100     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
101     HRESULT hr;
102
103     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
104             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
105
106     wined3d_mutex_lock();
107     hr = IWineD3DCubeTexture_SetPrivateData(This->wineD3DCubeTexture,refguid,pData,SizeOfData,Flags);
108     wined3d_mutex_unlock();
109
110     return hr;
111 }
112
113 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetPrivateData(LPDIRECT3DCUBETEXTURE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
114     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
115     HRESULT hr;
116
117     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
118             iface, debugstr_guid(refguid), pData, pSizeOfData);
119
120     wined3d_mutex_lock();
121     hr = IWineD3DCubeTexture_GetPrivateData(This->wineD3DCubeTexture,refguid,pData,pSizeOfData);
122     wined3d_mutex_unlock();
123
124     return hr;
125 }
126
127 static HRESULT WINAPI IDirect3DCubeTexture8Impl_FreePrivateData(LPDIRECT3DCUBETEXTURE8 iface, REFGUID refguid) {
128     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
129     HRESULT hr;
130
131     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
132
133     wined3d_mutex_lock();
134     hr = IWineD3DCubeTexture_FreePrivateData(This->wineD3DCubeTexture,refguid);
135     wined3d_mutex_unlock();
136
137     return hr;
138 }
139
140 static DWORD WINAPI IDirect3DCubeTexture8Impl_SetPriority(LPDIRECT3DCUBETEXTURE8 iface, DWORD PriorityNew) {
141     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
142     DWORD ret;
143
144     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
145
146     wined3d_mutex_lock();
147     ret = IWineD3DCubeTexture_SetPriority(This->wineD3DCubeTexture, PriorityNew);
148     wined3d_mutex_unlock();
149
150     return ret;
151 }
152
153 static DWORD WINAPI IDirect3DCubeTexture8Impl_GetPriority(LPDIRECT3DCUBETEXTURE8 iface) {
154     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
155     DWORD ret;
156
157     TRACE("iface %p.\n", iface);
158
159     wined3d_mutex_lock();
160     ret =  IWineD3DCubeTexture_GetPriority(This->wineD3DCubeTexture);
161     wined3d_mutex_unlock();
162
163     return ret;
164 }
165
166 static void WINAPI IDirect3DCubeTexture8Impl_PreLoad(LPDIRECT3DCUBETEXTURE8 iface) {
167     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
168
169     TRACE("iface %p.\n", iface);
170
171     wined3d_mutex_lock();
172     IWineD3DCubeTexture_PreLoad(This->wineD3DCubeTexture);
173     wined3d_mutex_unlock();
174 }
175
176 static D3DRESOURCETYPE WINAPI IDirect3DCubeTexture8Impl_GetType(LPDIRECT3DCUBETEXTURE8 iface) {
177     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
178     D3DRESOURCETYPE type;
179
180     TRACE("iface %p.\n", iface);
181
182     wined3d_mutex_lock();
183     type = IWineD3DCubeTexture_GetType(This->wineD3DCubeTexture);
184     wined3d_mutex_unlock();
185
186     return type;
187 }
188
189 /* IDirect3DCubeTexture8 IDirect3DBaseTexture8 Interface follow: */
190 static DWORD WINAPI IDirect3DCubeTexture8Impl_SetLOD(LPDIRECT3DCUBETEXTURE8 iface, DWORD LODNew) {
191     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
192     DWORD lod;
193
194     TRACE("iface %p, lod %u.\n", iface, LODNew);
195
196     wined3d_mutex_lock();
197     lod = IWineD3DCubeTexture_SetLOD(This->wineD3DCubeTexture, LODNew);
198     wined3d_mutex_unlock();
199
200     return lod;
201 }
202
203 static DWORD WINAPI IDirect3DCubeTexture8Impl_GetLOD(LPDIRECT3DCUBETEXTURE8 iface) {
204     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
205     DWORD lod;
206
207     TRACE("iface %p.\n", iface);
208
209     wined3d_mutex_lock();
210     lod = IWineD3DCubeTexture_GetLOD((LPDIRECT3DBASETEXTURE8) This);
211     wined3d_mutex_unlock();
212
213     return lod;
214 }
215
216 static DWORD WINAPI IDirect3DCubeTexture8Impl_GetLevelCount(LPDIRECT3DCUBETEXTURE8 iface) {
217     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
218     DWORD cnt;
219
220     TRACE("iface %p.\n", iface);
221
222     wined3d_mutex_lock();
223     cnt = IWineD3DCubeTexture_GetLevelCount(This->wineD3DCubeTexture);
224     wined3d_mutex_unlock();
225
226     return cnt;
227 }
228
229 /* IDirect3DCubeTexture8 Interface follow: */
230 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetLevelDesc(LPDIRECT3DCUBETEXTURE8 iface, UINT Level, D3DSURFACE_DESC *pDesc) {
231     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
232     HRESULT hr;
233     WINED3DSURFACE_DESC    wined3ddesc;
234
235     TRACE("iface %p, level %u, desc %p.\n", iface, Level, pDesc);
236
237     wined3d_mutex_lock();
238     hr = IWineD3DCubeTexture_GetLevelDesc(This->wineD3DCubeTexture, Level, &wined3ddesc);
239     wined3d_mutex_unlock();
240
241     if (SUCCEEDED(hr))
242     {
243         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
244         pDesc->Type = wined3ddesc.resource_type;
245         pDesc->Usage = wined3ddesc.usage;
246         pDesc->Pool = wined3ddesc.pool;
247         pDesc->Size = wined3ddesc.size;
248         pDesc->MultiSampleType = wined3ddesc.multisample_type;
249         pDesc->Width = wined3ddesc.width;
250         pDesc->Height = wined3ddesc.height;
251     }
252
253     return hr;
254 }
255
256 static HRESULT WINAPI IDirect3DCubeTexture8Impl_GetCubeMapSurface(IDirect3DCubeTexture8 *iface,
257         D3DCUBEMAP_FACES FaceType, UINT Level, IDirect3DSurface8 **ppCubeMapSurface)
258 {
259     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
260     IWineD3DSurface *mySurface = NULL;
261     HRESULT hr;
262
263     TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, FaceType, Level, ppCubeMapSurface);
264
265     wined3d_mutex_lock();
266     hr = IWineD3DCubeTexture_GetCubeMapSurface(This->wineD3DCubeTexture,
267             (WINED3DCUBEMAP_FACES) FaceType, Level, &mySurface);
268     if (SUCCEEDED(hr) && ppCubeMapSurface)
269     {
270        *ppCubeMapSurface = IWineD3DCubeTexture_GetParent(mySurface);
271        IDirect3DSurface8_AddRef(*ppCubeMapSurface);
272        IWineD3DCubeTexture_Release(mySurface);
273     }
274     wined3d_mutex_unlock();
275
276     return hr;
277 }
278
279 static HRESULT WINAPI IDirect3DCubeTexture8Impl_LockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT *pRect, DWORD Flags) {
280     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
281     HRESULT hr;
282
283     TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
284             iface, FaceType, Level, pLockedRect, pRect, Flags);
285
286     wined3d_mutex_lock();
287     hr = IWineD3DCubeTexture_LockRect(This->wineD3DCubeTexture, (WINED3DCUBEMAP_FACES) FaceType, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
288     wined3d_mutex_unlock();
289
290     return hr;
291 }
292
293 static HRESULT WINAPI IDirect3DCubeTexture8Impl_UnlockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
294     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
295     HRESULT hr;
296
297     TRACE("iface %p, face %#x, level %u.\n", iface, FaceType, Level);
298
299     wined3d_mutex_lock();
300     hr = IWineD3DCubeTexture_UnlockRect(This->wineD3DCubeTexture, (WINED3DCUBEMAP_FACES) FaceType, Level);
301     wined3d_mutex_unlock();
302
303     return hr;
304 }
305
306 static HRESULT WINAPI IDirect3DCubeTexture8Impl_AddDirtyRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, CONST RECT *pDirtyRect) {
307     IDirect3DCubeTexture8Impl *This = (IDirect3DCubeTexture8Impl *)iface;
308     HRESULT hr;
309
310     TRACE("iface %p, face %#x, dirty_rect %p.\n", iface, FaceType, pDirtyRect);
311
312     wined3d_mutex_lock();
313     hr = IWineD3DCubeTexture_AddDirtyRect(This->wineD3DCubeTexture, (WINED3DCUBEMAP_FACES) FaceType, pDirtyRect);
314     wined3d_mutex_unlock();
315
316     return hr;
317 }
318
319 static const IDirect3DCubeTexture8Vtbl Direct3DCubeTexture8_Vtbl =
320 {
321     /* IUnknown */
322     IDirect3DCubeTexture8Impl_QueryInterface,
323     IDirect3DCubeTexture8Impl_AddRef,
324     IDirect3DCubeTexture8Impl_Release,
325     /* IDirect3DResource8 */
326     IDirect3DCubeTexture8Impl_GetDevice,
327     IDirect3DCubeTexture8Impl_SetPrivateData,
328     IDirect3DCubeTexture8Impl_GetPrivateData,
329     IDirect3DCubeTexture8Impl_FreePrivateData,
330     IDirect3DCubeTexture8Impl_SetPriority,
331     IDirect3DCubeTexture8Impl_GetPriority,
332     IDirect3DCubeTexture8Impl_PreLoad,
333     IDirect3DCubeTexture8Impl_GetType,
334     /* IDirect3DBaseTexture8 */
335     IDirect3DCubeTexture8Impl_SetLOD,
336     IDirect3DCubeTexture8Impl_GetLOD,
337     IDirect3DCubeTexture8Impl_GetLevelCount,
338     /* IDirect3DCubeTexture8 */
339     IDirect3DCubeTexture8Impl_GetLevelDesc,
340     IDirect3DCubeTexture8Impl_GetCubeMapSurface,
341     IDirect3DCubeTexture8Impl_LockRect,
342     IDirect3DCubeTexture8Impl_UnlockRect,
343     IDirect3DCubeTexture8Impl_AddDirtyRect
344 };
345
346 static void STDMETHODCALLTYPE d3d8_cubetexture_wined3d_object_destroyed(void *parent)
347 {
348     HeapFree(GetProcessHeap(), 0, parent);
349 }
350
351 static const struct wined3d_parent_ops d3d8_cubetexture_wined3d_parent_ops =
352 {
353     d3d8_cubetexture_wined3d_object_destroyed,
354 };
355
356 HRESULT cubetexture_init(IDirect3DCubeTexture8Impl *texture, IDirect3DDevice8Impl *device,
357         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
358 {
359     HRESULT hr;
360
361     texture->lpVtbl = &Direct3DCubeTexture8_Vtbl;
362     texture->ref = 1;
363
364     wined3d_mutex_lock();
365     hr = IWineD3DDevice_CreateCubeTexture(device->WineD3DDevice, edge_length, levels,
366             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool, texture,
367             &d3d8_cubetexture_wined3d_parent_ops, &texture->wineD3DCubeTexture);
368     wined3d_mutex_unlock();
369     if (FAILED(hr))
370     {
371         WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
372         return hr;
373     }
374
375     texture->parentDevice = (IDirect3DDevice8 *)device;
376     IDirect3DDevice8_AddRef(texture->parentDevice);
377
378     return D3D_OK;
379 }