iphlpapi/tests: Fix crash on Vista.
[wine] / dlls / d3d9 / texture.c
1 /*
2  * IDirect3DTexture9 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 /* IDirect3DTexture9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DTexture9Impl_QueryInterface(LPDIRECT3DTEXTURE9 iface, REFIID riid, LPVOID* ppobj) {
30     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource9)
34         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
35         || IsEqualGUID(riid, &IID_IDirect3DTexture9)) {
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 IDirect3DTexture9Impl_AddRef(LPDIRECT3DTEXTURE9 iface) {
47     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
48     ULONG ref = InterlockedIncrement(&This->ref);
49
50     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
51
52     return ref;
53 }
54
55 static ULONG WINAPI IDirect3DTexture9Impl_Release(LPDIRECT3DTEXTURE9 iface) {
56     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
57     ULONG ref = InterlockedDecrement(&This->ref);
58
59     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
60
61     if (ref == 0) {
62         EnterCriticalSection(&d3d9_cs);
63         IWineD3DTexture_Destroy(This->wineD3DTexture, D3D9CB_DestroySurface);
64         LeaveCriticalSection(&d3d9_cs);
65         IUnknown_Release(This->parentDevice);
66         HeapFree(GetProcessHeap(), 0, This);
67     }
68     return ref;
69 }
70
71 /* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
72 static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(LPDIRECT3DTEXTURE9 iface, IDirect3DDevice9** ppDevice) {
73     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
74     HRESULT hr;
75     TRACE("(%p) Relay\n", This);
76
77     EnterCriticalSection(&d3d9_cs);
78     hr = IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
79     LeaveCriticalSection(&d3d9_cs);
80     return hr;
81 }
82
83 static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
84     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
85     HRESULT hr;
86     TRACE("(%p) Relay\n", This);
87
88     EnterCriticalSection(&d3d9_cs);
89     hr = IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
90     LeaveCriticalSection(&d3d9_cs);
91     return hr;
92 }
93
94 static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
95     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
96     HRESULT hr;
97     TRACE("(%p) Relay\n", This);
98
99     EnterCriticalSection(&d3d9_cs);
100     hr = IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
101     LeaveCriticalSection(&d3d9_cs);
102     return hr;
103 }
104
105 static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
106     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
107     HRESULT hr;
108     TRACE("(%p) Relay\n", This);
109
110     EnterCriticalSection(&d3d9_cs);
111     hr = IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
112     LeaveCriticalSection(&d3d9_cs);
113     return hr;
114 }
115
116 static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
117     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
118     DWORD ret;
119     TRACE("(%p) Relay\n", This);
120
121     EnterCriticalSection(&d3d9_cs);
122     ret = IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
123     LeaveCriticalSection(&d3d9_cs);
124     return ret;
125 }
126
127 static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
128     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
129     DWORD ret;
130     TRACE("(%p) Relay\n", This);
131
132     EnterCriticalSection(&d3d9_cs);
133     ret = IWineD3DTexture_GetPriority(This->wineD3DTexture);
134     LeaveCriticalSection(&d3d9_cs);
135     return ret;
136 }
137
138 static void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
139     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
140     TRACE("(%p) Relay\n", This);
141
142     EnterCriticalSection(&d3d9_cs);
143     IWineD3DTexture_PreLoad(This->wineD3DTexture);
144     LeaveCriticalSection(&d3d9_cs);
145 }
146
147 static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
148     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
149     HRESULT ret;
150     TRACE("(%p) Relay\n", This);
151
152     EnterCriticalSection(&d3d9_cs);
153     ret = IWineD3DTexture_GetType(This->wineD3DTexture);
154     LeaveCriticalSection(&d3d9_cs);
155     return ret;
156 }
157
158 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
159 static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
160     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
161     DWORD ret;
162     TRACE("(%p) Relay\n", This);
163
164     EnterCriticalSection(&d3d9_cs);
165     ret = IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
166     LeaveCriticalSection(&d3d9_cs);
167     return ret;
168 }
169
170 static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
171     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
172     DWORD ret;
173     TRACE("(%p) Relay\n", This);
174
175     EnterCriticalSection(&d3d9_cs);
176     ret = IWineD3DTexture_GetLOD(This->wineD3DTexture);
177     LeaveCriticalSection(&d3d9_cs);
178     return ret;
179 }
180
181 static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
182     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
183     DWORD ret;
184     TRACE("(%p) Relay\n", This);
185
186     EnterCriticalSection(&d3d9_cs);
187     ret = IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
188     LeaveCriticalSection(&d3d9_cs);
189     return ret;
190 }
191
192 static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
193     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
194     HRESULT hr;
195     TRACE("(%p) Relay\n", This);
196
197     EnterCriticalSection(&d3d9_cs);
198     hr = IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
199     LeaveCriticalSection(&d3d9_cs);
200     return hr;
201 }
202
203 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
204     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
205     D3DTEXTUREFILTERTYPE ret;
206     TRACE("(%p) Relay\n", This);
207
208     EnterCriticalSection(&d3d9_cs);
209     ret = (D3DTEXTUREFILTERTYPE) IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
210     LeaveCriticalSection(&d3d9_cs);
211     return ret;
212 }
213
214 static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
215     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
216     TRACE("(%p) Relay\n", This);
217
218     EnterCriticalSection(&d3d9_cs);
219     IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
220     LeaveCriticalSection(&d3d9_cs);
221 }
222
223 /* IDirect3DTexture9 Interface follow: */
224 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
225     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
226
227     WINED3DSURFACE_DESC    wined3ddesc;
228     UINT                   tmpInt = -1;
229     HRESULT                hr;
230     TRACE("(%p) Relay\n", This);
231
232     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
233     wined3ddesc.Format              = (WINED3DFORMAT *)&pDesc->Format;
234     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
235     wined3ddesc.Usage               = &pDesc->Usage;
236     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
237     wined3ddesc.Size                = &tmpInt; /* required for d3d8 */
238     wined3ddesc.MultiSampleType     = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
239     wined3ddesc.MultiSampleQuality  = &pDesc->MultiSampleQuality;
240     wined3ddesc.Width               = &pDesc->Width;
241     wined3ddesc.Height              = &pDesc->Height;
242
243     EnterCriticalSection(&d3d9_cs);
244     hr = IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
245     LeaveCriticalSection(&d3d9_cs);
246     return hr;
247 }
248
249 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE9 iface, UINT Level, IDirect3DSurface9** ppSurfaceLevel) {
250     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
251     HRESULT hrc = D3D_OK;
252     IWineD3DSurface *mySurface = NULL;
253
254     TRACE("(%p) Relay\n", This);
255     EnterCriticalSection(&d3d9_cs);
256     hrc = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
257     if (hrc == D3D_OK && NULL != ppSurfaceLevel) {
258        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppSurfaceLevel);
259        IWineD3DSurface_Release(mySurface);
260     }
261     LeaveCriticalSection(&d3d9_cs);
262     return hrc;
263 }
264
265 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
266     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
267     HRESULT hr;
268     TRACE("(%p) Relay\n", This);
269
270     EnterCriticalSection(&d3d9_cs);
271     hr = IWineD3DTexture_LockRect(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
272     LeaveCriticalSection(&d3d9_cs);
273     return hr;
274 }
275
276 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
277     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
278     HRESULT hr;
279     TRACE("(%p) Relay\n", This);
280
281     EnterCriticalSection(&d3d9_cs);
282     hr = IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
283     LeaveCriticalSection(&d3d9_cs);
284     return hr;
285 }
286
287 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
288     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
289     HRESULT hr;
290     TRACE("(%p) Relay\n", This);
291
292     EnterCriticalSection(&d3d9_cs);
293     hr = IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
294     LeaveCriticalSection(&d3d9_cs);
295     return hr;
296 }
297
298 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
299 {
300     /* IUnknown */
301     IDirect3DTexture9Impl_QueryInterface,
302     IDirect3DTexture9Impl_AddRef,
303     IDirect3DTexture9Impl_Release,
304      /* IDirect3DResource9 */
305     IDirect3DTexture9Impl_GetDevice,
306     IDirect3DTexture9Impl_SetPrivateData,
307     IDirect3DTexture9Impl_GetPrivateData,
308     IDirect3DTexture9Impl_FreePrivateData,
309     IDirect3DTexture9Impl_SetPriority,
310     IDirect3DTexture9Impl_GetPriority,
311     IDirect3DTexture9Impl_PreLoad,
312     IDirect3DTexture9Impl_GetType,
313     /* IDirect3dBaseTexture9 */
314     IDirect3DTexture9Impl_SetLOD,
315     IDirect3DTexture9Impl_GetLOD,
316     IDirect3DTexture9Impl_GetLevelCount,
317     IDirect3DTexture9Impl_SetAutoGenFilterType,
318     IDirect3DTexture9Impl_GetAutoGenFilterType,
319     IDirect3DTexture9Impl_GenerateMipSubLevels,
320     /* IDirect3DTexture9 */
321     IDirect3DTexture9Impl_GetLevelDesc,
322     IDirect3DTexture9Impl_GetSurfaceLevel,
323     IDirect3DTexture9Impl_LockRect,
324     IDirect3DTexture9Impl_UnlockRect,
325     IDirect3DTexture9Impl_AddDirtyRect
326 };
327
328
329 /* IDirect3DDevice9 IDirect3DTexture9 Methods follow: */
330 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateTexture(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
331                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
332     IDirect3DTexture9Impl *object;
333     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
334     HRESULT hrc = D3D_OK;
335     
336     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%d), Fmt(%#x), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
337
338     /* Allocate the storage for the device */
339     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture9Impl));
340
341     if (NULL == object) {
342         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
343         return D3DERR_OUTOFVIDEOMEMORY;
344     }
345
346     object->lpVtbl = &Direct3DTexture9_Vtbl;
347     object->ref = 1;
348     EnterCriticalSection(&d3d9_cs);
349     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage & WINED3DUSAGE_MASK,
350                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DTexture, pSharedHandle, (IUnknown *)object, D3D9CB_CreateSurface);
351     LeaveCriticalSection(&d3d9_cs);
352
353     if (FAILED(hrc)) {
354
355         /* free up object */
356         FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
357         HeapFree(GetProcessHeap(), 0, object);
358    } else {
359         IUnknown_AddRef(iface);
360         object->parentDevice = iface;
361         *ppTexture= (LPDIRECT3DTEXTURE9) object;
362         TRACE("(%p) Created Texture %p, %p\n", This, object, object->wineD3DTexture);
363    }
364
365    return hrc;
366 }