comdlg32: Add IServiceProvider implementation to the Item Dialog.
[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         wined3d_texture_incref(This->wined3d_texture);
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         wined3d_texture_decref(This->wined3d_texture);
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 = wined3d_texture_set_private_data(This->wined3d_texture, 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 = wined3d_texture_get_private_data(This->wined3d_texture, 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 = wined3d_texture_free_private_data(This->wined3d_texture, 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 = wined3d_texture_set_priority(This->wined3d_texture, 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 = wined3d_texture_get_priority(This->wined3d_texture);
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     wined3d_texture_preload(This->wined3d_texture);
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 = wined3d_texture_get_type(This->wined3d_texture);
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 = wined3d_texture_set_lod(This->wined3d_texture, 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 = wined3d_texture_get_lod(This->wined3d_texture);
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 = wined3d_texture_get_level_count(This->wined3d_texture);
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 = wined3d_texture_set_autogen_filter_type(This->wined3d_texture, (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)wined3d_texture_get_autogen_filter_type(This->wined3d_texture);
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     wined3d_texture_generate_mipmaps(This->wined3d_texture);
262     wined3d_mutex_unlock();
263 }
264
265 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(IDirect3DTexture9 *iface,
266         UINT level, D3DSURFACE_DESC *desc)
267 {
268     IDirect3DTexture9Impl *texture = (IDirect3DTexture9Impl *)iface;
269     struct wined3d_resource *sub_resource;
270     HRESULT hr = D3D_OK;
271
272     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
273
274     wined3d_mutex_lock();
275     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
276         hr = D3DERR_INVALIDCALL;
277     else
278     {
279         struct wined3d_resource_desc wined3d_desc;
280
281         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
282         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
283         desc->Type = wined3d_desc.resource_type;
284         desc->Usage = wined3d_desc.usage;
285         desc->Pool = wined3d_desc.pool;
286         desc->MultiSampleType = wined3d_desc.multisample_type;
287         desc->MultiSampleQuality = wined3d_desc.multisample_quality;
288         desc->Width = wined3d_desc.width;
289         desc->Height = wined3d_desc.height;
290     }
291     wined3d_mutex_unlock();
292
293     return hr;
294 }
295
296 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(IDirect3DTexture9 *iface,
297         UINT level, IDirect3DSurface9 **surface)
298 {
299     IDirect3DTexture9Impl *texture = (IDirect3DTexture9Impl *)iface;
300     struct wined3d_resource *sub_resource;
301
302     TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
303
304     wined3d_mutex_lock();
305     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
306     {
307         wined3d_mutex_unlock();
308         return D3DERR_INVALIDCALL;
309     }
310
311     *surface = wined3d_resource_get_parent(sub_resource);
312     IDirect3DSurface9_AddRef(*surface);
313     wined3d_mutex_unlock();
314
315     return D3D_OK;
316 }
317
318 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(IDirect3DTexture9 *iface,
319         UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
320 {
321     IDirect3DTexture9Impl *texture = (IDirect3DTexture9Impl *)iface;
322     struct wined3d_resource *sub_resource;
323     HRESULT hr;
324
325     TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
326             iface, level, locked_rect, rect, flags);
327
328     wined3d_mutex_lock();
329     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
330         hr = D3DERR_INVALIDCALL;
331     else
332         hr = IDirect3DSurface9_LockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource),
333                 locked_rect, rect, flags);
334     wined3d_mutex_unlock();
335
336     return hr;
337 }
338
339 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(IDirect3DTexture9 *iface, UINT level)
340 {
341     IDirect3DTexture9Impl *texture = (IDirect3DTexture9Impl *)iface;
342     struct wined3d_resource *sub_resource;
343     HRESULT hr;
344
345     TRACE("iface %p, level %u.\n", iface, level);
346
347     wined3d_mutex_lock();
348     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
349         hr = D3DERR_INVALIDCALL;
350     else
351         hr = IDirect3DSurface9_UnlockRect((IDirect3DSurface9 *)wined3d_resource_get_parent(sub_resource));
352     wined3d_mutex_unlock();
353
354     return hr;
355 }
356
357 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(IDirect3DTexture9 *iface, const RECT *dirty_rect)
358 {
359     IDirect3DTexture9Impl *texture = (IDirect3DTexture9Impl *)iface;
360     HRESULT hr;
361
362     TRACE("iface %p, dirty_rect %s.\n",
363             iface, wine_dbgstr_rect(dirty_rect));
364
365     wined3d_mutex_lock();
366     if (!dirty_rect)
367         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, NULL);
368     else
369     {
370         WINED3DBOX dirty_region;
371
372         dirty_region.Left = dirty_rect->left;
373         dirty_region.Top = dirty_rect->top;
374         dirty_region.Right = dirty_rect->right;
375         dirty_region.Bottom = dirty_rect->bottom;
376         dirty_region.Front = 0;
377         dirty_region.Back = 1;
378         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, &dirty_region);
379     }
380     wined3d_mutex_unlock();
381
382     return hr;
383 }
384
385 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
386 {
387     /* IUnknown */
388     IDirect3DTexture9Impl_QueryInterface,
389     IDirect3DTexture9Impl_AddRef,
390     IDirect3DTexture9Impl_Release,
391      /* IDirect3DResource9 */
392     IDirect3DTexture9Impl_GetDevice,
393     IDirect3DTexture9Impl_SetPrivateData,
394     IDirect3DTexture9Impl_GetPrivateData,
395     IDirect3DTexture9Impl_FreePrivateData,
396     IDirect3DTexture9Impl_SetPriority,
397     IDirect3DTexture9Impl_GetPriority,
398     IDirect3DTexture9Impl_PreLoad,
399     IDirect3DTexture9Impl_GetType,
400     /* IDirect3dBaseTexture9 */
401     IDirect3DTexture9Impl_SetLOD,
402     IDirect3DTexture9Impl_GetLOD,
403     IDirect3DTexture9Impl_GetLevelCount,
404     IDirect3DTexture9Impl_SetAutoGenFilterType,
405     IDirect3DTexture9Impl_GetAutoGenFilterType,
406     IDirect3DTexture9Impl_GenerateMipSubLevels,
407     /* IDirect3DTexture9 */
408     IDirect3DTexture9Impl_GetLevelDesc,
409     IDirect3DTexture9Impl_GetSurfaceLevel,
410     IDirect3DTexture9Impl_LockRect,
411     IDirect3DTexture9Impl_UnlockRect,
412     IDirect3DTexture9Impl_AddDirtyRect
413 };
414
415 static void STDMETHODCALLTYPE d3d9_texture_wined3d_object_destroyed(void *parent)
416 {
417     HeapFree(GetProcessHeap(), 0, parent);
418 }
419
420 static const struct wined3d_parent_ops d3d9_texture_wined3d_parent_ops =
421 {
422     d3d9_texture_wined3d_object_destroyed,
423 };
424
425 HRESULT texture_init(IDirect3DTexture9Impl *texture, IDirect3DDevice9Impl *device,
426         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
427 {
428     HRESULT hr;
429
430     texture->lpVtbl = &Direct3DTexture9_Vtbl;
431     texture->ref = 1;
432
433     wined3d_mutex_lock();
434     hr = IWineD3DDevice_CreateTexture(device->WineD3DDevice, width, height, levels,
435             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool,
436             texture, &d3d9_texture_wined3d_parent_ops, &texture->wined3d_texture);
437     wined3d_mutex_unlock();
438     if (FAILED(hr))
439     {
440         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
441         return hr;
442     }
443
444     texture->parentDevice = (IDirect3DDevice9Ex *)device;
445     IDirect3DDevice9Ex_AddRef(texture->parentDevice);
446
447     return D3D_OK;
448 }