spoolss/tests: Change skip() to win_skip().
[wine] / dlls / d3d8 / texture.c
1 /*
2  * IDirect3DTexture8 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 /* IDirect3DTexture8 IUnknown parts follow: */
27 static inline IDirect3DTexture8Impl *impl_from_IDirect3DTexture8(IDirect3DTexture8 *iface)
28 {
29     return CONTAINING_RECORD(iface, IDirect3DTexture8Impl, IDirect3DTexture8_iface);
30 }
31
32 static HRESULT WINAPI IDirect3DTexture8Impl_QueryInterface(IDirect3DTexture8 *iface, REFIID riid,
33         void **ppobj)
34 {
35     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
36
37     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
38
39     if (IsEqualGUID(riid, &IID_IUnknown)
40         || IsEqualGUID(riid, &IID_IDirect3DResource8)
41         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
42         || IsEqualGUID(riid, &IID_IDirect3DTexture8)) {
43         IUnknown_AddRef(iface);
44         *ppobj = This;
45         return S_OK;
46     }
47
48     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
49     *ppobj = NULL;
50     return E_NOINTERFACE;
51 }
52
53 static ULONG WINAPI IDirect3DTexture8Impl_AddRef(IDirect3DTexture8 *iface)
54 {
55     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
56     ULONG ref = InterlockedIncrement(&This->ref);
57
58     TRACE("%p increasing refcount to %u.\n", iface, ref);
59
60     if (ref == 1)
61     {
62         IDirect3DDevice8_AddRef(This->parentDevice);
63         wined3d_mutex_lock();
64         IWineD3DTexture_AddRef(This->wineD3DTexture);
65         wined3d_mutex_unlock();
66     }
67
68     return ref;
69 }
70
71 static ULONG WINAPI IDirect3DTexture8Impl_Release(IDirect3DTexture8 *iface)
72 {
73     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
74     ULONG ref = InterlockedDecrement(&This->ref);
75
76     TRACE("%p decreasing refcount to %u.\n", iface, ref);
77
78     if (ref == 0) {
79         IDirect3DDevice8 *parentDevice = This->parentDevice;
80
81         wined3d_mutex_lock();
82         IWineD3DTexture_Release(This->wineD3DTexture);
83         wined3d_mutex_unlock();
84
85         /* Release the device last, as it may cause the device to be destroyed. */
86         IDirect3DDevice8_Release(parentDevice);
87     }
88     return ref;
89 }
90
91 /* IDirect3DTexture8 IDirect3DResource8 Interface follow: */
92 static HRESULT WINAPI IDirect3DTexture8Impl_GetDevice(IDirect3DTexture8 *iface,
93         IDirect3DDevice8 **device)
94 {
95     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
96
97     TRACE("iface %p, device %p.\n", iface, device);
98
99     *device = (IDirect3DDevice8 *)This->parentDevice;
100     IDirect3DDevice8_AddRef(*device);
101
102     TRACE("Returning device %p.\n", *device);
103
104     return D3D_OK;
105 }
106
107 static HRESULT WINAPI IDirect3DTexture8Impl_SetPrivateData(IDirect3DTexture8 *iface,
108         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
109 {
110     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
111     HRESULT hr;
112
113     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
114             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
115
116     wined3d_mutex_lock();
117     hr = IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
118     wined3d_mutex_unlock();
119
120     return hr;
121 }
122
123 static HRESULT WINAPI IDirect3DTexture8Impl_GetPrivateData(IDirect3DTexture8 *iface,
124         REFGUID refguid, void *pData, DWORD *pSizeOfData)
125 {
126     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
127     HRESULT hr;
128
129     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
130             iface, debugstr_guid(refguid), pData, pSizeOfData);
131
132     wined3d_mutex_lock();
133     hr = IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
134     wined3d_mutex_unlock();
135
136     return hr;
137 }
138
139 static HRESULT WINAPI IDirect3DTexture8Impl_FreePrivateData(IDirect3DTexture8 *iface,
140         REFGUID refguid)
141 {
142     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
143     HRESULT hr;
144
145     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
146
147     wined3d_mutex_lock();
148     hr = IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
149     wined3d_mutex_unlock();
150
151     return hr;
152 }
153
154 static DWORD WINAPI IDirect3DTexture8Impl_SetPriority(IDirect3DTexture8 *iface, DWORD PriorityNew)
155 {
156     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
157     DWORD ret;
158
159     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
160
161     wined3d_mutex_lock();
162     ret = IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
163     wined3d_mutex_unlock();
164
165     return ret;
166 }
167
168 static DWORD WINAPI IDirect3DTexture8Impl_GetPriority(IDirect3DTexture8 *iface)
169 {
170     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
171     DWORD ret;
172
173     TRACE("iface %p.\n", iface);
174
175     wined3d_mutex_lock();
176     ret = IWineD3DTexture_GetPriority(This->wineD3DTexture);
177     wined3d_mutex_unlock();
178
179     return ret;
180 }
181
182 static void WINAPI IDirect3DTexture8Impl_PreLoad(IDirect3DTexture8 *iface)
183 {
184     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
185
186     TRACE("iface %p.\n", iface);
187
188     wined3d_mutex_lock();
189     IWineD3DTexture_PreLoad(This->wineD3DTexture);
190     wined3d_mutex_unlock();
191 }
192
193 static D3DRESOURCETYPE WINAPI IDirect3DTexture8Impl_GetType(IDirect3DTexture8 *iface)
194 {
195     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
196     D3DRESOURCETYPE type;
197
198     TRACE("iface %p.\n", iface);
199
200     wined3d_mutex_lock();
201     type = IWineD3DTexture_GetType(This->wineD3DTexture);
202     wined3d_mutex_unlock();
203
204     return type;
205 }
206
207 /* IDirect3DTexture8 IDirect3DBaseTexture8 Interface follow: */
208 static DWORD WINAPI IDirect3DTexture8Impl_SetLOD(IDirect3DTexture8 *iface, DWORD LODNew)
209 {
210     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
211     DWORD ret;
212
213     TRACE("iface %p, lod %u.\n", iface, LODNew);
214
215     wined3d_mutex_lock();
216     ret = IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
217     wined3d_mutex_unlock();
218
219     return ret;
220 }
221
222 static DWORD WINAPI IDirect3DTexture8Impl_GetLOD(IDirect3DTexture8 *iface)
223 {
224     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
225     DWORD ret;
226
227     TRACE("iface %p.\n", iface);
228
229     wined3d_mutex_lock();
230     ret = IWineD3DTexture_GetLOD(This->wineD3DTexture);
231     wined3d_mutex_unlock();
232
233     return ret;
234 }
235
236 static DWORD WINAPI IDirect3DTexture8Impl_GetLevelCount(IDirect3DTexture8 *iface)
237 {
238     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
239     DWORD ret;
240
241     TRACE("iface %p.\n", iface);
242
243     wined3d_mutex_lock();
244     ret = IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
245     wined3d_mutex_unlock();
246
247     return ret;
248 }
249
250 /* IDirect3DTexture8 Interface follow: */
251 static HRESULT WINAPI IDirect3DTexture8Impl_GetLevelDesc(IDirect3DTexture8 *iface, UINT Level,
252         D3DSURFACE_DESC *pDesc)
253 {
254     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
255     WINED3DSURFACE_DESC wined3ddesc;
256     HRESULT hr;
257
258     TRACE("iface %p, level %u, desc %p.\n", iface, Level, pDesc);
259
260     wined3d_mutex_lock();
261     hr = IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
262     wined3d_mutex_unlock();
263
264     if (SUCCEEDED(hr))
265     {
266         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
267         pDesc->Type = wined3ddesc.resource_type;
268         pDesc->Usage = wined3ddesc.usage;
269         pDesc->Pool = wined3ddesc.pool;
270         pDesc->Size = wined3ddesc.size;
271         pDesc->MultiSampleType = wined3ddesc.multisample_type;
272         pDesc->Width = wined3ddesc.width;
273         pDesc->Height = wined3ddesc.height;
274     }
275
276     return hr;
277 }
278
279 static HRESULT WINAPI IDirect3DTexture8Impl_GetSurfaceLevel(IDirect3DTexture8 *iface,
280         UINT Level, IDirect3DSurface8 **ppSurfaceLevel)
281 {
282     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
283     IWineD3DSurface *mySurface = NULL;
284     HRESULT hr;
285
286     TRACE("iface %p, level %u, surface %p.\n", iface, Level, ppSurfaceLevel);
287
288     wined3d_mutex_lock();
289     hr = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
290     if (SUCCEEDED(hr) && ppSurfaceLevel)
291     {
292        *ppSurfaceLevel = IWineD3DSurface_GetParent(mySurface);
293        IDirect3DSurface8_AddRef(*ppSurfaceLevel);
294        IWineD3DSurface_Release(mySurface);
295     }
296     wined3d_mutex_unlock();
297
298     return hr;
299 }
300
301 static HRESULT WINAPI IDirect3DTexture8Impl_LockRect(IDirect3DTexture8 *iface, UINT Level,
302         D3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD Flags)
303 {
304     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
305     HRESULT hr;
306
307     TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
308             iface, Level, pLockedRect, pRect, Flags);
309
310     wined3d_mutex_lock();
311     hr = IWineD3DTexture_Map(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *)pLockedRect, pRect, Flags);
312     wined3d_mutex_unlock();
313
314     return hr;
315 }
316
317 static HRESULT WINAPI IDirect3DTexture8Impl_UnlockRect(IDirect3DTexture8 *iface, UINT Level)
318 {
319     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
320     HRESULT hr;
321
322     TRACE("iface %p, level %u.\n", iface, Level);
323
324     wined3d_mutex_lock();
325     hr = IWineD3DTexture_Unmap(This->wineD3DTexture, Level);
326     wined3d_mutex_unlock();
327
328     return hr;
329 }
330
331 static HRESULT WINAPI IDirect3DTexture8Impl_AddDirtyRect(IDirect3DTexture8 *iface,
332         const RECT *pDirtyRect)
333 {
334     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
335     HRESULT hr;
336
337     TRACE("iface %p, dirty_rect %p.\n", iface, pDirtyRect);
338
339     wined3d_mutex_lock();
340     hr = IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
341     wined3d_mutex_unlock();
342
343     return hr;
344 }
345
346 static const IDirect3DTexture8Vtbl Direct3DTexture8_Vtbl =
347 {
348     /* IUnknown */
349     IDirect3DTexture8Impl_QueryInterface,
350     IDirect3DTexture8Impl_AddRef,
351     IDirect3DTexture8Impl_Release,
352      /* IDirect3DResource8 */
353     IDirect3DTexture8Impl_GetDevice,
354     IDirect3DTexture8Impl_SetPrivateData,
355     IDirect3DTexture8Impl_GetPrivateData,
356     IDirect3DTexture8Impl_FreePrivateData,
357     IDirect3DTexture8Impl_SetPriority,
358     IDirect3DTexture8Impl_GetPriority,
359     IDirect3DTexture8Impl_PreLoad,
360     IDirect3DTexture8Impl_GetType,
361     /* IDirect3dBaseTexture8 */
362     IDirect3DTexture8Impl_SetLOD,
363     IDirect3DTexture8Impl_GetLOD,
364     IDirect3DTexture8Impl_GetLevelCount,
365     /* IDirect3DTexture8 */
366     IDirect3DTexture8Impl_GetLevelDesc,
367     IDirect3DTexture8Impl_GetSurfaceLevel,
368     IDirect3DTexture8Impl_LockRect,
369     IDirect3DTexture8Impl_UnlockRect,
370     IDirect3DTexture8Impl_AddDirtyRect
371 };
372
373 static void STDMETHODCALLTYPE d3d8_texture_wined3d_object_destroyed(void *parent)
374 {
375     HeapFree(GetProcessHeap(), 0, parent);
376 }
377
378 static const struct wined3d_parent_ops d3d8_texture_wined3d_parent_ops =
379 {
380     d3d8_texture_wined3d_object_destroyed,
381 };
382
383 HRESULT texture_init(IDirect3DTexture8Impl *texture, IDirect3DDevice8Impl *device,
384         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
385 {
386     HRESULT hr;
387
388     texture->IDirect3DTexture8_iface.lpVtbl = &Direct3DTexture8_Vtbl;
389     texture->ref = 1;
390
391     wined3d_mutex_lock();
392     hr = IWineD3DDevice_CreateTexture(device->WineD3DDevice, width, height, levels,
393             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool,
394             texture, &d3d8_texture_wined3d_parent_ops, &texture->wineD3DTexture);
395     wined3d_mutex_unlock();
396     if (FAILED(hr))
397     {
398         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
399         return hr;
400     }
401
402     texture->parentDevice = &device->IDirect3DDevice8_iface;
403     IDirect3DDevice8_AddRef(texture->parentDevice);
404
405     return D3D_OK;
406 }