urlmon: Skip more tests on too old IEs.
[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     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
33
34     if (IsEqualGUID(riid, &IID_IUnknown)
35         || IsEqualGUID(riid, &IID_IDirect3DResource9)
36         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
37         || IsEqualGUID(riid, &IID_IDirect3DTexture9)) {
38         IDirect3DTexture9_AddRef(iface);
39         *ppobj = This;
40         return S_OK;
41     }
42
43     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
44     *ppobj = NULL;
45     return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI IDirect3DTexture9Impl_AddRef(LPDIRECT3DTEXTURE9 iface) {
49     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
50     ULONG ref = InterlockedIncrement(&This->ref);
51
52     TRACE("%p increasing refcount to %u.\n", iface, ref);
53
54     if (ref == 1)
55     {
56         IDirect3DDevice9Ex_AddRef(This->parentDevice);
57         wined3d_mutex_lock();
58         IWineD3DTexture_AddRef(This->wineD3DTexture);
59         wined3d_mutex_unlock();
60     }
61
62     return ref;
63 }
64
65 static ULONG WINAPI IDirect3DTexture9Impl_Release(LPDIRECT3DTEXTURE9 iface) {
66     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
67     ULONG ref = InterlockedDecrement(&This->ref);
68
69     TRACE("%p decreasing refcount to %u.\n", iface, ref);
70
71     if (ref == 0) {
72         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
73
74         wined3d_mutex_lock();
75         IWineD3DTexture_Release(This->wineD3DTexture);
76         wined3d_mutex_unlock();
77
78         /* Release the device last, as it may cause the device to be destroyed. */
79         IDirect3DDevice9Ex_Release(parentDevice);
80     }
81     return ref;
82 }
83
84 /* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
85 static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(IDirect3DTexture9 *iface, IDirect3DDevice9 **device)
86 {
87     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
88
89     TRACE("iface %p, device %p.\n", iface, device);
90
91     *device = (IDirect3DDevice9 *)This->parentDevice;
92     IDirect3DDevice9_AddRef(*device);
93
94     TRACE("Returning device %p.\n", *device);
95
96     return D3D_OK;
97 }
98
99 static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
100     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)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 = IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
108     wined3d_mutex_unlock();
109
110     return hr;
111 }
112
113 static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
114     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)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 = IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
122     wined3d_mutex_unlock();
123
124     return hr;
125 }
126
127 static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
128     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
129     HRESULT hr;
130
131     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
132
133     wined3d_mutex_lock();
134     hr = IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
135     wined3d_mutex_unlock();
136
137     return hr;
138 }
139
140 static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
141     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
142     DWORD ret;
143
144     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
145
146     wined3d_mutex_lock();
147     ret = IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
148     wined3d_mutex_unlock();
149
150     return ret;
151 }
152
153 static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
154     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
155     DWORD ret;
156
157     TRACE("iface %p.\n", iface);
158
159     wined3d_mutex_lock();
160     ret = IWineD3DTexture_GetPriority(This->wineD3DTexture);
161     wined3d_mutex_unlock();
162
163     return ret;
164 }
165
166 static void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
167     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
168
169     TRACE("iface %p.\n", iface);
170
171     wined3d_mutex_lock();
172     IWineD3DTexture_PreLoad(This->wineD3DTexture);
173     wined3d_mutex_unlock();
174 }
175
176 static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
177     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
178     HRESULT ret;
179
180     TRACE("iface %p.\n", iface);
181
182     wined3d_mutex_lock();
183     ret = IWineD3DTexture_GetType(This->wineD3DTexture);
184     wined3d_mutex_unlock();
185
186     return ret;
187 }
188
189 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
190 static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
191     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
192     DWORD ret;
193
194     TRACE("iface %p, lod %u.\n", iface, LODNew);
195
196     wined3d_mutex_lock();
197     ret = IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
198     wined3d_mutex_unlock();
199
200     return ret;
201 }
202
203 static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
204     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
205     DWORD ret;
206
207     TRACE("iface %p.\n", iface);
208
209     wined3d_mutex_lock();
210     ret = IWineD3DTexture_GetLOD(This->wineD3DTexture);
211     wined3d_mutex_unlock();
212
213     return ret;
214 }
215
216 static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
217     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
218     DWORD ret;
219
220     TRACE("iface %p.\n", iface);
221
222     wined3d_mutex_lock();
223     ret = IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
224     wined3d_mutex_unlock();
225
226     return ret;
227 }
228
229 static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
230     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
231     HRESULT hr;
232
233     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
234
235     wined3d_mutex_lock();
236     hr = IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
237     wined3d_mutex_unlock();
238
239     return hr;
240 }
241
242 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
243     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
244     D3DTEXTUREFILTERTYPE ret;
245
246     TRACE("iface %p.\n", iface);
247
248     wined3d_mutex_lock();
249     ret = (D3DTEXTUREFILTERTYPE) IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
250     wined3d_mutex_unlock();
251
252     return ret;
253 }
254
255 static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
256     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
257
258     TRACE("iface %p.\n", iface);
259
260     wined3d_mutex_lock();
261     IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
262     wined3d_mutex_unlock();
263 }
264
265 /* IDirect3DTexture9 Interface follow: */
266 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
267     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
268     WINED3DSURFACE_DESC wined3ddesc;
269     HRESULT hr;
270
271     TRACE("iface %p, level %u, desc %p.\n", iface, Level, pDesc);
272
273     wined3d_mutex_lock();
274     hr = IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
275     wined3d_mutex_unlock();
276
277     if (SUCCEEDED(hr))
278     {
279         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
280         pDesc->Type = wined3ddesc.resource_type;
281         pDesc->Usage = wined3ddesc.usage;
282         pDesc->Pool = wined3ddesc.pool;
283         pDesc->MultiSampleType = wined3ddesc.multisample_type;
284         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
285         pDesc->Width = wined3ddesc.width;
286         pDesc->Height = wined3ddesc.height;
287     }
288
289     return hr;
290 }
291
292 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(IDirect3DTexture9 *iface,
293         UINT Level, IDirect3DSurface9 **ppSurfaceLevel)
294 {
295     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
296     IWineD3DSurface *mySurface = NULL;
297     HRESULT hr;
298
299     TRACE("iface %p, level %u, surface %p.\n", iface, Level, ppSurfaceLevel);
300
301     wined3d_mutex_lock();
302     hr = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
303     if (SUCCEEDED(hr) && ppSurfaceLevel)
304     {
305         *ppSurfaceLevel = IWineD3DSurface_GetParent(mySurface);
306         IDirect3DSurface9_AddRef(*ppSurfaceLevel);
307         IWineD3DSurface_Release(mySurface);
308     }
309     wined3d_mutex_unlock();
310
311     return hr;
312 }
313
314 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
315     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
316     HRESULT hr;
317
318     TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
319             iface, Level, pLockedRect, pRect, Flags);
320
321     wined3d_mutex_lock();
322     hr = IWineD3DTexture_LockRect(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
323     wined3d_mutex_unlock();
324
325     return hr;
326 }
327
328 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
329     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
330     HRESULT hr;
331
332     TRACE("iface %p, level %u.\n", iface, Level);
333
334     wined3d_mutex_lock();
335     hr = IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
336     wined3d_mutex_unlock();
337
338     return hr;
339 }
340
341 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
342     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
343     HRESULT hr;
344
345     TRACE("iface %p, dirty_rect %p.\n", iface, pDirtyRect);
346
347     wined3d_mutex_lock();
348     hr = IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
349     wined3d_mutex_unlock();
350
351     return hr;
352 }
353
354 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
355 {
356     /* IUnknown */
357     IDirect3DTexture9Impl_QueryInterface,
358     IDirect3DTexture9Impl_AddRef,
359     IDirect3DTexture9Impl_Release,
360      /* IDirect3DResource9 */
361     IDirect3DTexture9Impl_GetDevice,
362     IDirect3DTexture9Impl_SetPrivateData,
363     IDirect3DTexture9Impl_GetPrivateData,
364     IDirect3DTexture9Impl_FreePrivateData,
365     IDirect3DTexture9Impl_SetPriority,
366     IDirect3DTexture9Impl_GetPriority,
367     IDirect3DTexture9Impl_PreLoad,
368     IDirect3DTexture9Impl_GetType,
369     /* IDirect3dBaseTexture9 */
370     IDirect3DTexture9Impl_SetLOD,
371     IDirect3DTexture9Impl_GetLOD,
372     IDirect3DTexture9Impl_GetLevelCount,
373     IDirect3DTexture9Impl_SetAutoGenFilterType,
374     IDirect3DTexture9Impl_GetAutoGenFilterType,
375     IDirect3DTexture9Impl_GenerateMipSubLevels,
376     /* IDirect3DTexture9 */
377     IDirect3DTexture9Impl_GetLevelDesc,
378     IDirect3DTexture9Impl_GetSurfaceLevel,
379     IDirect3DTexture9Impl_LockRect,
380     IDirect3DTexture9Impl_UnlockRect,
381     IDirect3DTexture9Impl_AddDirtyRect
382 };
383
384 static void STDMETHODCALLTYPE d3d9_texture_wined3d_object_destroyed(void *parent)
385 {
386     HeapFree(GetProcessHeap(), 0, parent);
387 }
388
389 static const struct wined3d_parent_ops d3d9_texture_wined3d_parent_ops =
390 {
391     d3d9_texture_wined3d_object_destroyed,
392 };
393
394 HRESULT texture_init(IDirect3DTexture9Impl *texture, IDirect3DDevice9Impl *device,
395         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
396 {
397     HRESULT hr;
398
399     texture->lpVtbl = &Direct3DTexture9_Vtbl;
400     texture->ref = 1;
401
402     wined3d_mutex_lock();
403     hr = IWineD3DDevice_CreateTexture(device->WineD3DDevice, width, height, levels,
404             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool,
405             texture, &d3d9_texture_wined3d_parent_ops, &texture->wineD3DTexture);
406     wined3d_mutex_unlock();
407     if (FAILED(hr))
408     {
409         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
410         return hr;
411     }
412
413     texture->parentDevice = (IDirect3DDevice9Ex *)device;
414     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
415
416     return D3D_OK;
417 }