shell32: Add support of KF_REDIRECT_DEL_SOURCE_CONTENTS flag to redirection.
[wine] / dlls / d3d10core / texture.c
1 /*
2  * Copyright 2009 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "d3d10core_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d10core);
26
27 static inline struct d3d10_texture2d *impl_from_ID3D10Texture2D(ID3D10Texture2D *iface)
28 {
29     return CONTAINING_RECORD(iface, struct d3d10_texture2d, ID3D10Texture2D_iface);
30 }
31
32 /* IUnknown methods */
33
34 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_QueryInterface(ID3D10Texture2D *iface, REFIID riid, void **object)
35 {
36     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
37
38     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
39
40     if (IsEqualGUID(riid, &IID_ID3D10Texture2D)
41             || IsEqualGUID(riid, &IID_ID3D10Resource)
42             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
43             || IsEqualGUID(riid, &IID_IUnknown))
44     {
45         IUnknown_AddRef(iface);
46         *object = iface;
47         return S_OK;
48     }
49
50     if (This->dxgi_surface)
51     {
52         TRACE("Forwarding to dxgi surface\n");
53         return IDXGISurface_QueryInterface(This->dxgi_surface, riid, object);
54     }
55
56     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
57
58     *object = NULL;
59     return E_NOINTERFACE;
60 }
61
62 static ULONG STDMETHODCALLTYPE d3d10_texture2d_AddRef(ID3D10Texture2D *iface)
63 {
64     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
65     ULONG refcount = InterlockedIncrement(&This->refcount);
66
67     TRACE("%p increasing refcount to %u\n", This, refcount);
68
69     if (refcount == 1 && This->wined3d_surface)
70         wined3d_surface_incref(This->wined3d_surface);
71
72     return refcount;
73 }
74
75 static void STDMETHODCALLTYPE d3d10_texture2d_wined3d_object_released(void *parent)
76 {
77     struct d3d10_texture2d *This = parent;
78
79     if (This->dxgi_surface) IDXGISurface_Release(This->dxgi_surface);
80     HeapFree(GetProcessHeap(), 0, This);
81 }
82
83 static ULONG STDMETHODCALLTYPE d3d10_texture2d_Release(ID3D10Texture2D *iface)
84 {
85     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
86     ULONG refcount = InterlockedDecrement(&This->refcount);
87
88     TRACE("%p decreasing refcount to %u\n", This, refcount);
89
90     if (!refcount)
91     {
92         if (This->wined3d_surface)
93             wined3d_surface_decref(This->wined3d_surface);
94         else
95             d3d10_texture2d_wined3d_object_released(This);
96     }
97
98     return refcount;
99 }
100
101 /* ID3D10DeviceChild methods */
102
103 static void STDMETHODCALLTYPE d3d10_texture2d_GetDevice(ID3D10Texture2D *iface, ID3D10Device **device)
104 {
105     FIXME("iface %p, device %p stub!\n", iface, device);
106 }
107
108 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_GetPrivateData(ID3D10Texture2D *iface,
109         REFGUID guid, UINT *data_size, void *data)
110 {
111     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
112             iface, debugstr_guid(guid), data_size, data);
113
114     return E_NOTIMPL;
115 }
116
117 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateData(ID3D10Texture2D *iface,
118         REFGUID guid, UINT data_size, const void *data)
119 {
120     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
121             iface, debugstr_guid(guid), data_size, data);
122
123     return E_NOTIMPL;
124 }
125
126 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateDataInterface(ID3D10Texture2D *iface,
127         REFGUID guid, const IUnknown *data)
128 {
129     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
130
131     return E_NOTIMPL;
132 }
133
134 /* ID3D10Resource methods */
135
136 static void STDMETHODCALLTYPE d3d10_texture2d_GetType(ID3D10Texture2D *iface,
137         D3D10_RESOURCE_DIMENSION *resource_dimension)
138 {
139     TRACE("iface %p, resource_dimension %p\n", iface, resource_dimension);
140
141     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
142 }
143
144 static void STDMETHODCALLTYPE d3d10_texture2d_SetEvictionPriority(ID3D10Texture2D *iface, UINT eviction_priority)
145 {
146     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
147 }
148
149 static UINT STDMETHODCALLTYPE d3d10_texture2d_GetEvictionPriority(ID3D10Texture2D *iface)
150 {
151     FIXME("iface %p stub!\n", iface);
152
153     return 0;
154 }
155
156 /* ID3D10Texture2D methods */
157
158 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_Map(ID3D10Texture2D *iface, UINT sub_resource,
159         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE2D *mapped_texture)
160 {
161     FIXME("iface %p, sub_resource %u, map_type %u, map_flags %#x, mapped_texture %p stub!\n",
162             iface, sub_resource, map_type, map_flags, mapped_texture);
163
164     return E_NOTIMPL;
165 }
166
167 static void STDMETHODCALLTYPE d3d10_texture2d_Unmap(ID3D10Texture2D *iface, UINT sub_resource)
168 {
169     FIXME("iface %p, sub_resource %u stub!\n", iface, sub_resource);
170 }
171
172 static void STDMETHODCALLTYPE d3d10_texture2d_GetDesc(ID3D10Texture2D *iface, D3D10_TEXTURE2D_DESC *desc)
173 {
174     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
175
176     TRACE("iface %p, desc %p\n", iface, desc);
177
178     *desc = This->desc;
179 }
180
181 static const struct ID3D10Texture2DVtbl d3d10_texture2d_vtbl =
182 {
183     /* IUnknown methods */
184     d3d10_texture2d_QueryInterface,
185     d3d10_texture2d_AddRef,
186     d3d10_texture2d_Release,
187     /* ID3D10DeviceChild methods */
188     d3d10_texture2d_GetDevice,
189     d3d10_texture2d_GetPrivateData,
190     d3d10_texture2d_SetPrivateData,
191     d3d10_texture2d_SetPrivateDataInterface,
192     /* ID3D10Resource methods */
193     d3d10_texture2d_GetType,
194     d3d10_texture2d_SetEvictionPriority,
195     d3d10_texture2d_GetEvictionPriority,
196     /* ID3D10Texture2D methods */
197     d3d10_texture2d_Map,
198     d3d10_texture2d_Unmap,
199     d3d10_texture2d_GetDesc,
200 };
201
202 static const struct wined3d_parent_ops d3d10_texture2d_wined3d_parent_ops =
203 {
204     d3d10_texture2d_wined3d_object_released,
205 };
206
207 HRESULT d3d10_texture2d_init(struct d3d10_texture2d *texture, struct d3d10_device *device,
208         const D3D10_TEXTURE2D_DESC *desc)
209 {
210     HRESULT hr;
211
212     texture->ID3D10Texture2D_iface.lpVtbl = &d3d10_texture2d_vtbl;
213     texture->refcount = 1;
214     texture->desc = *desc;
215
216     if (desc->MipLevels == 1 && desc->ArraySize == 1)
217     {
218         IWineDXGIDevice *wine_device;
219
220         hr = ID3D10Device_QueryInterface((ID3D10Device *)device, &IID_IWineDXGIDevice, (void **)&wine_device);
221         if (FAILED(hr))
222         {
223             ERR("Device should implement IWineDXGIDevice\n");
224             return E_FAIL;
225         }
226
227         hr = IWineDXGIDevice_create_surface(wine_device, NULL, 0, NULL,
228                 (IUnknown *)&texture->ID3D10Texture2D_iface, (void **)&texture->dxgi_surface);
229         IWineDXGIDevice_Release(wine_device);
230         if (FAILED(hr))
231         {
232             ERR("Failed to create DXGI surface, returning %#x\n", hr);
233             return hr;
234         }
235
236         FIXME("Implement DXGI<->wined3d usage conversion\n");
237
238         hr = wined3d_surface_create(device->wined3d_device, desc->Width, desc->Height,
239                 wined3dformat_from_dxgi_format(desc->Format), FALSE, FALSE, 0, desc->Usage, WINED3DPOOL_DEFAULT,
240                 desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3DMULTISAMPLE_NONE,
241                 desc->SampleDesc.Quality, SURFACE_OPENGL, texture, &d3d10_texture2d_wined3d_parent_ops,
242                 &texture->wined3d_surface);
243         if (FAILED(hr))
244         {
245             ERR("CreateSurface failed, returning %#x\n", hr);
246             IDXGISurface_Release(texture->dxgi_surface);
247             return hr;
248         }
249     }
250
251     return S_OK;
252 }
253
254 static inline struct d3d10_texture3d *impl_from_ID3D10Texture3D(ID3D10Texture3D *iface)
255 {
256     return CONTAINING_RECORD(iface, struct d3d10_texture3d, ID3D10Texture3D_iface);
257 }
258
259 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_QueryInterface(ID3D10Texture3D *iface, REFIID riid, void **object)
260 {
261     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
262
263     if (IsEqualGUID(riid, &IID_ID3D10Texture3D)
264             || IsEqualGUID(riid, &IID_ID3D10Resource)
265             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
266             || IsEqualGUID(riid, &IID_IUnknown))
267     {
268         IUnknown_AddRef(iface);
269         *object = iface;
270         return S_OK;
271     }
272
273     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
274
275     *object = NULL;
276     return E_NOINTERFACE;
277 }
278
279 static ULONG STDMETHODCALLTYPE d3d10_texture3d_AddRef(ID3D10Texture3D *iface)
280 {
281     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
282     ULONG refcount = InterlockedIncrement(&texture->refcount);
283
284     TRACE("%p increasing refcount to %u.\n", texture, refcount);
285
286     if (refcount == 1)
287         wined3d_texture_incref(texture->wined3d_texture);
288
289     return refcount;
290 }
291
292 static void STDMETHODCALLTYPE d3d10_texture3d_wined3d_object_released(void *parent)
293 {
294     HeapFree(GetProcessHeap(), 0, parent);
295 }
296
297 static ULONG STDMETHODCALLTYPE d3d10_texture3d_Release(ID3D10Texture3D *iface)
298 {
299     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
300     ULONG refcount = InterlockedDecrement(&texture->refcount);
301
302     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
303
304     if (!refcount)
305         wined3d_texture_decref(texture->wined3d_texture);
306
307     return refcount;
308 }
309
310 static void STDMETHODCALLTYPE d3d10_texture3d_GetDevice(ID3D10Texture3D *iface, ID3D10Device **device)
311 {
312     FIXME("iface %p, device %p stub!\n", iface, device);
313 }
314
315 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_GetPrivateData(ID3D10Texture3D *iface,
316         REFGUID guid, UINT *data_size, void *data)
317 {
318     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
319             iface, debugstr_guid(guid), data_size, data);
320
321     return E_NOTIMPL;
322 }
323
324 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateData(ID3D10Texture3D *iface,
325         REFGUID guid, UINT data_size, const void *data)
326 {
327     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
328             iface, debugstr_guid(guid), data_size, data);
329
330     return E_NOTIMPL;
331 }
332
333 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateDataInterface(ID3D10Texture3D *iface,
334         REFGUID guid, const IUnknown *data)
335 {
336     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
337
338     return E_NOTIMPL;
339 }
340
341 static void STDMETHODCALLTYPE d3d10_texture3d_GetType(ID3D10Texture3D *iface,
342         D3D10_RESOURCE_DIMENSION *resource_dimension)
343 {
344     TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
345
346     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE3D;
347 }
348
349 static void STDMETHODCALLTYPE d3d10_texture3d_SetEvictionPriority(ID3D10Texture3D *iface, UINT eviction_priority)
350 {
351     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
352 }
353
354 static UINT STDMETHODCALLTYPE d3d10_texture3d_GetEvictionPriority(ID3D10Texture3D *iface)
355 {
356     FIXME("iface %p stub!\n", iface);
357
358     return 0;
359 }
360
361 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_Map(ID3D10Texture3D *iface, UINT sub_resource_idx,
362         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE3D *mapped_texture)
363 {
364     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
365     struct wined3d_resource *sub_resource;
366     WINED3DLOCKED_BOX wined3d_map_desc;
367     HRESULT hr;
368
369     TRACE("iface %p, sub_resource_idx %u, map_type %u, map_flags %#x, mapped_texture %p.\n",
370             iface, sub_resource_idx, map_type, map_flags, mapped_texture);
371
372     if (map_type != D3D10_MAP_READ_WRITE)
373         FIXME("Ignoring map_type %#x.\n", map_type);
374     if (map_flags)
375         FIXME("Ignoring map_flags %#x.\n", map_flags);
376
377     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
378         hr = E_INVALIDARG;
379     else if (SUCCEEDED(hr = wined3d_volume_map(wined3d_volume_from_resource(sub_resource),
380             &wined3d_map_desc, NULL, 0)))
381     {
382         mapped_texture->pData = wined3d_map_desc.pBits;
383         mapped_texture->RowPitch = wined3d_map_desc.RowPitch;
384         mapped_texture->DepthPitch = wined3d_map_desc.SlicePitch;
385     }
386
387     return hr;
388 }
389
390 static void STDMETHODCALLTYPE d3d10_texture3d_Unmap(ID3D10Texture3D *iface, UINT sub_resource_idx)
391 {
392     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
393     struct wined3d_resource *sub_resource;
394
395     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
396
397     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
398         return;
399
400     wined3d_volume_unmap(wined3d_volume_from_resource(sub_resource));
401 }
402
403 static void STDMETHODCALLTYPE d3d10_texture3d_GetDesc(ID3D10Texture3D *iface, D3D10_TEXTURE3D_DESC *desc)
404 {
405     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
406
407     TRACE("iface %p, desc %p.\n", iface, desc);
408
409     *desc = texture->desc;
410 }
411
412 static const struct ID3D10Texture3DVtbl d3d10_texture3d_vtbl =
413 {
414     /* IUnknown methods */
415     d3d10_texture3d_QueryInterface,
416     d3d10_texture3d_AddRef,
417     d3d10_texture3d_Release,
418     /* ID3D10DeviceChild methods */
419     d3d10_texture3d_GetDevice,
420     d3d10_texture3d_GetPrivateData,
421     d3d10_texture3d_SetPrivateData,
422     d3d10_texture3d_SetPrivateDataInterface,
423     /* ID3D10Resource methods */
424     d3d10_texture3d_GetType,
425     d3d10_texture3d_SetEvictionPriority,
426     d3d10_texture3d_GetEvictionPriority,
427     /* ID3D10Texture3D methods */
428     d3d10_texture3d_Map,
429     d3d10_texture3d_Unmap,
430     d3d10_texture3d_GetDesc,
431 };
432
433 static const struct wined3d_parent_ops d3d10_texture3d_wined3d_parent_ops =
434 {
435     d3d10_texture3d_wined3d_object_released,
436 };
437
438 HRESULT d3d10_texture3d_init(struct d3d10_texture3d *texture, struct d3d10_device *device,
439         const D3D10_TEXTURE3D_DESC *desc)
440 {
441     HRESULT hr;
442
443     texture->ID3D10Texture3D_iface.lpVtbl = &d3d10_texture3d_vtbl;
444     texture->refcount = 1;
445     texture->desc = *desc;
446
447     FIXME("Implement DXGI<->wined3d usage conversion.\n");
448
449     hr = wined3d_texture_create_3d(device->wined3d_device, desc->Width, desc->Height, desc->Depth,
450             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3DPOOL_DEFAULT,
451             texture, &d3d10_texture3d_wined3d_parent_ops, &texture->wined3d_texture);
452     if (FAILED(hr))
453     {
454         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
455         return hr;
456     }
457
458     return S_OK;
459 }