wined3d: Remove an obsolete comment.
[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 static inline IDirect3DTexture9Impl *impl_from_IDirect3DTexture9(IDirect3DTexture9 *iface)
29 {
30     return CONTAINING_RECORD(iface, IDirect3DTexture9Impl, IDirect3DTexture9_iface);
31 }
32
33 static HRESULT WINAPI IDirect3DTexture9Impl_QueryInterface(IDirect3DTexture9 *iface, REFIID riid,
34         void **ppobj)
35 {
36     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
37
38     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
39
40     if (IsEqualGUID(riid, &IID_IUnknown)
41         || IsEqualGUID(riid, &IID_IDirect3DResource9)
42         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
43         || IsEqualGUID(riid, &IID_IDirect3DTexture9)) {
44         IDirect3DTexture9_AddRef(iface);
45         *ppobj = This;
46         return S_OK;
47     }
48
49     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
50     *ppobj = NULL;
51     return E_NOINTERFACE;
52 }
53
54 static ULONG WINAPI IDirect3DTexture9Impl_AddRef(IDirect3DTexture9 *iface)
55 {
56     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
57     ULONG ref = InterlockedIncrement(&This->ref);
58
59     TRACE("%p increasing refcount to %u.\n", iface, ref);
60
61     if (ref == 1)
62     {
63         IDirect3DDevice9Ex_AddRef(This->parentDevice);
64         wined3d_mutex_lock();
65         wined3d_texture_incref(This->wined3d_texture);
66         wined3d_mutex_unlock();
67     }
68
69     return ref;
70 }
71
72 static ULONG WINAPI IDirect3DTexture9Impl_Release(IDirect3DTexture9 *iface)
73 {
74     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
75     ULONG ref = InterlockedDecrement(&This->ref);
76
77     TRACE("%p decreasing refcount to %u.\n", iface, ref);
78
79     if (ref == 0) {
80         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
81
82         wined3d_mutex_lock();
83         wined3d_texture_decref(This->wined3d_texture);
84         wined3d_mutex_unlock();
85
86         /* Release the device last, as it may cause the device to be destroyed. */
87         IDirect3DDevice9Ex_Release(parentDevice);
88     }
89     return ref;
90 }
91
92 static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(IDirect3DTexture9 *iface,
93         IDirect3DDevice9 **device)
94 {
95     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
96
97     TRACE("iface %p, device %p.\n", iface, device);
98
99     *device = (IDirect3DDevice9 *)This->parentDevice;
100     IDirect3DDevice9_AddRef(*device);
101
102     TRACE("Returning device %p.\n", *device);
103
104     return D3D_OK;
105 }
106
107 static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(IDirect3DTexture9 *iface,
108         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
109 {
110     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
111     struct wined3d_resource *resource;
112     HRESULT hr;
113
114     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
115             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
116
117     wined3d_mutex_lock();
118     resource = wined3d_texture_get_resource(This->wined3d_texture);
119     hr = wined3d_resource_set_private_data(resource, refguid, pData, SizeOfData, Flags);
120     wined3d_mutex_unlock();
121
122     return hr;
123 }
124
125 static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(IDirect3DTexture9 *iface,
126         REFGUID refguid, void *pData, DWORD *pSizeOfData)
127 {
128     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
129     struct wined3d_resource *resource;
130     HRESULT hr;
131
132     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
133             iface, debugstr_guid(refguid), pData, pSizeOfData);
134
135     wined3d_mutex_lock();
136     resource = wined3d_texture_get_resource(This->wined3d_texture);
137     hr = wined3d_resource_get_private_data(resource, refguid, pData, pSizeOfData);
138     wined3d_mutex_unlock();
139
140     return hr;
141 }
142
143 static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(IDirect3DTexture9 *iface,
144         REFGUID refguid)
145 {
146     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
147     struct wined3d_resource *resource;
148     HRESULT hr;
149
150     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
151
152     wined3d_mutex_lock();
153     resource = wined3d_texture_get_resource(This->wined3d_texture);
154     hr = wined3d_resource_free_private_data(resource, refguid);
155     wined3d_mutex_unlock();
156
157     return hr;
158 }
159
160 static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(IDirect3DTexture9 *iface, DWORD PriorityNew)
161 {
162     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
163     DWORD ret;
164
165     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
166
167     wined3d_mutex_lock();
168     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
169     wined3d_mutex_unlock();
170
171     return ret;
172 }
173
174 static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(IDirect3DTexture9 *iface)
175 {
176     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
177     DWORD ret;
178
179     TRACE("iface %p.\n", iface);
180
181     wined3d_mutex_lock();
182     ret = wined3d_texture_get_priority(This->wined3d_texture);
183     wined3d_mutex_unlock();
184
185     return ret;
186 }
187
188 static void WINAPI IDirect3DTexture9Impl_PreLoad(IDirect3DTexture9 *iface)
189 {
190     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
191
192     TRACE("iface %p.\n", iface);
193
194     wined3d_mutex_lock();
195     wined3d_texture_preload(This->wined3d_texture);
196     wined3d_mutex_unlock();
197 }
198
199 static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(IDirect3DTexture9 *iface)
200 {
201     TRACE("iface %p.\n", iface);
202
203     return D3DRTYPE_TEXTURE;
204 }
205
206 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
207 static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(IDirect3DTexture9 *iface, DWORD LODNew)
208 {
209     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
210     DWORD ret;
211
212     TRACE("iface %p, lod %u.\n", iface, LODNew);
213
214     wined3d_mutex_lock();
215     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
216     wined3d_mutex_unlock();
217
218     return ret;
219 }
220
221 static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(IDirect3DTexture9 *iface)
222 {
223     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
224     DWORD ret;
225
226     TRACE("iface %p.\n", iface);
227
228     wined3d_mutex_lock();
229     ret = wined3d_texture_get_lod(This->wined3d_texture);
230     wined3d_mutex_unlock();
231
232     return ret;
233 }
234
235 static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(IDirect3DTexture9 *iface)
236 {
237     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
238     DWORD ret;
239
240     TRACE("iface %p.\n", iface);
241
242     wined3d_mutex_lock();
243     ret = wined3d_texture_get_level_count(This->wined3d_texture);
244     wined3d_mutex_unlock();
245
246     return ret;
247 }
248
249 static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(IDirect3DTexture9 *iface,
250         D3DTEXTUREFILTERTYPE FilterType)
251 {
252     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
253     HRESULT hr;
254
255     TRACE("iface %p, filter_type %#x.\n", iface, FilterType);
256
257     wined3d_mutex_lock();
258     hr = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (WINED3DTEXTUREFILTERTYPE)FilterType);
259     wined3d_mutex_unlock();
260
261     return hr;
262 }
263
264 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(IDirect3DTexture9 *iface)
265 {
266     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
267     D3DTEXTUREFILTERTYPE ret;
268
269     TRACE("iface %p.\n", iface);
270
271     wined3d_mutex_lock();
272     ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
273     wined3d_mutex_unlock();
274
275     return ret;
276 }
277
278 static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(IDirect3DTexture9 *iface)
279 {
280     IDirect3DTexture9Impl *This = impl_from_IDirect3DTexture9(iface);
281
282     TRACE("iface %p.\n", iface);
283
284     wined3d_mutex_lock();
285     wined3d_texture_generate_mipmaps(This->wined3d_texture);
286     wined3d_mutex_unlock();
287 }
288
289 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(IDirect3DTexture9 *iface,
290         UINT level, D3DSURFACE_DESC *desc)
291 {
292     IDirect3DTexture9Impl *texture = impl_from_IDirect3DTexture9(iface);
293     struct wined3d_resource *sub_resource;
294     HRESULT hr = D3D_OK;
295
296     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
297
298     wined3d_mutex_lock();
299     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
300         hr = D3DERR_INVALIDCALL;
301     else
302     {
303         struct wined3d_resource_desc wined3d_desc;
304
305         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
306         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
307         desc->Type = wined3d_desc.resource_type;
308         desc->Usage = wined3d_desc.usage;
309         desc->Pool = wined3d_desc.pool;
310         desc->MultiSampleType = wined3d_desc.multisample_type;
311         desc->MultiSampleQuality = wined3d_desc.multisample_quality;
312         desc->Width = wined3d_desc.width;
313         desc->Height = wined3d_desc.height;
314     }
315     wined3d_mutex_unlock();
316
317     return hr;
318 }
319
320 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(IDirect3DTexture9 *iface,
321         UINT level, IDirect3DSurface9 **surface)
322 {
323     IDirect3DTexture9Impl *texture = impl_from_IDirect3DTexture9(iface);
324     struct wined3d_resource *sub_resource;
325
326     TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
327
328     wined3d_mutex_lock();
329     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
330     {
331         wined3d_mutex_unlock();
332         return D3DERR_INVALIDCALL;
333     }
334
335     *surface = wined3d_resource_get_parent(sub_resource);
336     IDirect3DSurface9_AddRef(*surface);
337     wined3d_mutex_unlock();
338
339     return D3D_OK;
340 }
341
342 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(IDirect3DTexture9 *iface,
343         UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
344 {
345     IDirect3DTexture9Impl *texture = impl_from_IDirect3DTexture9(iface);
346     struct wined3d_resource *sub_resource;
347     HRESULT hr;
348
349     TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
350             iface, level, locked_rect, rect, flags);
351
352     wined3d_mutex_lock();
353     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
354         hr = D3DERR_INVALIDCALL;
355     else
356         hr = IDirect3DSurface9_LockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource),
357                 locked_rect, rect, flags);
358     wined3d_mutex_unlock();
359
360     return hr;
361 }
362
363 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(IDirect3DTexture9 *iface, UINT level)
364 {
365     IDirect3DTexture9Impl *texture = impl_from_IDirect3DTexture9(iface);
366     struct wined3d_resource *sub_resource;
367     HRESULT hr;
368
369     TRACE("iface %p, level %u.\n", iface, level);
370
371     wined3d_mutex_lock();
372     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
373         hr = D3DERR_INVALIDCALL;
374     else
375         hr = IDirect3DSurface9_UnlockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource));
376     wined3d_mutex_unlock();
377
378     return hr;
379 }
380
381 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(IDirect3DTexture9 *iface,
382         const RECT *dirty_rect)
383 {
384     IDirect3DTexture9Impl *texture = impl_from_IDirect3DTexture9(iface);
385     HRESULT hr;
386
387     TRACE("iface %p, dirty_rect %s.\n",
388             iface, wine_dbgstr_rect(dirty_rect));
389
390     wined3d_mutex_lock();
391     if (!dirty_rect)
392         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, NULL);
393     else
394     {
395         WINED3DBOX dirty_region;
396
397         dirty_region.Left = dirty_rect->left;
398         dirty_region.Top = dirty_rect->top;
399         dirty_region.Right = dirty_rect->right;
400         dirty_region.Bottom = dirty_rect->bottom;
401         dirty_region.Front = 0;
402         dirty_region.Back = 1;
403         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, &dirty_region);
404     }
405     wined3d_mutex_unlock();
406
407     return hr;
408 }
409
410 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
411 {
412     /* IUnknown */
413     IDirect3DTexture9Impl_QueryInterface,
414     IDirect3DTexture9Impl_AddRef,
415     IDirect3DTexture9Impl_Release,
416      /* IDirect3DResource9 */
417     IDirect3DTexture9Impl_GetDevice,
418     IDirect3DTexture9Impl_SetPrivateData,
419     IDirect3DTexture9Impl_GetPrivateData,
420     IDirect3DTexture9Impl_FreePrivateData,
421     IDirect3DTexture9Impl_SetPriority,
422     IDirect3DTexture9Impl_GetPriority,
423     IDirect3DTexture9Impl_PreLoad,
424     IDirect3DTexture9Impl_GetType,
425     /* IDirect3dBaseTexture9 */
426     IDirect3DTexture9Impl_SetLOD,
427     IDirect3DTexture9Impl_GetLOD,
428     IDirect3DTexture9Impl_GetLevelCount,
429     IDirect3DTexture9Impl_SetAutoGenFilterType,
430     IDirect3DTexture9Impl_GetAutoGenFilterType,
431     IDirect3DTexture9Impl_GenerateMipSubLevels,
432     /* IDirect3DTexture9 */
433     IDirect3DTexture9Impl_GetLevelDesc,
434     IDirect3DTexture9Impl_GetSurfaceLevel,
435     IDirect3DTexture9Impl_LockRect,
436     IDirect3DTexture9Impl_UnlockRect,
437     IDirect3DTexture9Impl_AddDirtyRect
438 };
439
440 static void STDMETHODCALLTYPE d3d9_texture_wined3d_object_destroyed(void *parent)
441 {
442     HeapFree(GetProcessHeap(), 0, parent);
443 }
444
445 static const struct wined3d_parent_ops d3d9_texture_wined3d_parent_ops =
446 {
447     d3d9_texture_wined3d_object_destroyed,
448 };
449
450 HRESULT texture_init(IDirect3DTexture9Impl *texture, IDirect3DDevice9Impl *device,
451         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
452 {
453     HRESULT hr;
454
455     texture->IDirect3DTexture9_iface.lpVtbl = &Direct3DTexture9_Vtbl;
456     texture->ref = 1;
457
458     wined3d_mutex_lock();
459     hr = wined3d_texture_create_2d(device->wined3d_device, width, height, levels,
460             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool,
461             texture, &d3d9_texture_wined3d_parent_ops, &texture->wined3d_texture);
462     wined3d_mutex_unlock();
463     if (FAILED(hr))
464     {
465         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
466         return hr;
467     }
468
469     texture->parentDevice = &device->IDirect3DDevice9Ex_iface;
470     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
471
472     return D3D_OK;
473 }