oledb32: Check start for NULL before strstrW (Coverity).
[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 IUnknown_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)
70         wined3d_texture_incref(This->wined3d_texture);
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) IUnknown_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         wined3d_texture_decref(This->wined3d_texture);
92
93     return refcount;
94 }
95
96 /* ID3D10DeviceChild methods */
97
98 static void STDMETHODCALLTYPE d3d10_texture2d_GetDevice(ID3D10Texture2D *iface, ID3D10Device **device)
99 {
100     FIXME("iface %p, device %p stub!\n", iface, device);
101 }
102
103 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_GetPrivateData(ID3D10Texture2D *iface,
104         REFGUID guid, UINT *data_size, void *data)
105 {
106     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
107             iface, debugstr_guid(guid), data_size, data);
108
109     return E_NOTIMPL;
110 }
111
112 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateData(ID3D10Texture2D *iface,
113         REFGUID guid, UINT data_size, const void *data)
114 {
115     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
116             iface, debugstr_guid(guid), data_size, data);
117
118     return E_NOTIMPL;
119 }
120
121 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_SetPrivateDataInterface(ID3D10Texture2D *iface,
122         REFGUID guid, const IUnknown *data)
123 {
124     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
125
126     return E_NOTIMPL;
127 }
128
129 /* ID3D10Resource methods */
130
131 static void STDMETHODCALLTYPE d3d10_texture2d_GetType(ID3D10Texture2D *iface,
132         D3D10_RESOURCE_DIMENSION *resource_dimension)
133 {
134     TRACE("iface %p, resource_dimension %p\n", iface, resource_dimension);
135
136     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
137 }
138
139 static void STDMETHODCALLTYPE d3d10_texture2d_SetEvictionPriority(ID3D10Texture2D *iface, UINT eviction_priority)
140 {
141     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
142 }
143
144 static UINT STDMETHODCALLTYPE d3d10_texture2d_GetEvictionPriority(ID3D10Texture2D *iface)
145 {
146     FIXME("iface %p stub!\n", iface);
147
148     return 0;
149 }
150
151 /* ID3D10Texture2D methods */
152
153 static HRESULT STDMETHODCALLTYPE d3d10_texture2d_Map(ID3D10Texture2D *iface, UINT sub_resource_idx,
154         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE2D *mapped_texture)
155 {
156     struct d3d10_texture2d *texture = impl_from_ID3D10Texture2D(iface);
157     struct wined3d_map_desc wined3d_map_desc;
158     struct wined3d_resource *sub_resource;
159     HRESULT hr;
160
161     TRACE("iface %p, sub_resource_idx %u, map_type %u, map_flags %#x, mapped_texture %p.\n",
162             iface, sub_resource_idx, map_type, map_flags, mapped_texture);
163
164     if (map_type != D3D10_MAP_READ_WRITE)
165         FIXME("Ignoring map_type %#x.\n", map_type);
166     if (map_flags)
167         FIXME("Ignoring map_flags %#x.\n", map_flags);
168
169     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
170         hr = E_INVALIDARG;
171     else if (SUCCEEDED(hr = wined3d_surface_map(wined3d_surface_from_resource(sub_resource),
172             &wined3d_map_desc, NULL, 0)))
173     {
174         mapped_texture->pData = wined3d_map_desc.data;
175         mapped_texture->RowPitch = wined3d_map_desc.row_pitch;
176     }
177
178     return hr;
179 }
180
181 static void STDMETHODCALLTYPE d3d10_texture2d_Unmap(ID3D10Texture2D *iface, UINT sub_resource_idx)
182 {
183     struct d3d10_texture2d *texture = impl_from_ID3D10Texture2D(iface);
184     struct wined3d_resource *sub_resource;
185
186     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
187
188     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
189         return;
190
191     wined3d_surface_unmap(wined3d_surface_from_resource(sub_resource));
192 }
193
194 static void STDMETHODCALLTYPE d3d10_texture2d_GetDesc(ID3D10Texture2D *iface, D3D10_TEXTURE2D_DESC *desc)
195 {
196     struct d3d10_texture2d *This = impl_from_ID3D10Texture2D(iface);
197
198     TRACE("iface %p, desc %p\n", iface, desc);
199
200     *desc = This->desc;
201 }
202
203 static const struct ID3D10Texture2DVtbl d3d10_texture2d_vtbl =
204 {
205     /* IUnknown methods */
206     d3d10_texture2d_QueryInterface,
207     d3d10_texture2d_AddRef,
208     d3d10_texture2d_Release,
209     /* ID3D10DeviceChild methods */
210     d3d10_texture2d_GetDevice,
211     d3d10_texture2d_GetPrivateData,
212     d3d10_texture2d_SetPrivateData,
213     d3d10_texture2d_SetPrivateDataInterface,
214     /* ID3D10Resource methods */
215     d3d10_texture2d_GetType,
216     d3d10_texture2d_SetEvictionPriority,
217     d3d10_texture2d_GetEvictionPriority,
218     /* ID3D10Texture2D methods */
219     d3d10_texture2d_Map,
220     d3d10_texture2d_Unmap,
221     d3d10_texture2d_GetDesc,
222 };
223
224 static const struct wined3d_parent_ops d3d10_texture2d_wined3d_parent_ops =
225 {
226     d3d10_texture2d_wined3d_object_released,
227 };
228
229 HRESULT d3d10_texture2d_init(struct d3d10_texture2d *texture, struct d3d10_device *device,
230         const D3D10_TEXTURE2D_DESC *desc)
231 {
232     HRESULT hr;
233
234     texture->ID3D10Texture2D_iface.lpVtbl = &d3d10_texture2d_vtbl;
235     texture->refcount = 1;
236     texture->desc = *desc;
237
238     if (desc->MipLevels == 1 && desc->ArraySize == 1)
239     {
240         IWineDXGIDevice *wine_device;
241
242         hr = ID3D10Device_QueryInterface(&device->ID3D10Device_iface, &IID_IWineDXGIDevice,
243                 (void **)&wine_device);
244         if (FAILED(hr))
245         {
246             ERR("Device should implement IWineDXGIDevice\n");
247             return E_FAIL;
248         }
249
250         hr = IWineDXGIDevice_create_surface(wine_device, NULL, 0, NULL,
251                 (IUnknown *)&texture->ID3D10Texture2D_iface, (void **)&texture->dxgi_surface);
252         IWineDXGIDevice_Release(wine_device);
253         if (FAILED(hr))
254         {
255             ERR("Failed to create DXGI surface, returning %#x\n", hr);
256             return hr;
257         }
258     }
259
260     FIXME("Implement DXGI<->wined3d usage conversion\n");
261     if (desc->ArraySize != 1)
262         FIXME("Array textures not implemented.\n");
263     if (desc->SampleDesc.Count > 1)
264         FIXME("Multisampled textures not implemented.\n");
265
266     hr = wined3d_texture_create_2d(device->wined3d_device, desc->Width, desc->Height,
267             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3D_POOL_DEFAULT,
268             texture, &d3d10_texture2d_wined3d_parent_ops, &texture->wined3d_texture);
269     if (FAILED(hr))
270     {
271         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
272         if (texture->dxgi_surface)
273             IUnknown_Release(texture->dxgi_surface);
274         return hr;
275     }
276     texture->desc.MipLevels = wined3d_texture_get_level_count(texture->wined3d_texture);
277
278     return S_OK;
279 }
280
281 static inline struct d3d10_texture3d *impl_from_ID3D10Texture3D(ID3D10Texture3D *iface)
282 {
283     return CONTAINING_RECORD(iface, struct d3d10_texture3d, ID3D10Texture3D_iface);
284 }
285
286 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_QueryInterface(ID3D10Texture3D *iface, REFIID riid, void **object)
287 {
288     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
289
290     if (IsEqualGUID(riid, &IID_ID3D10Texture3D)
291             || IsEqualGUID(riid, &IID_ID3D10Resource)
292             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
293             || IsEqualGUID(riid, &IID_IUnknown))
294     {
295         IUnknown_AddRef(iface);
296         *object = iface;
297         return S_OK;
298     }
299
300     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
301
302     *object = NULL;
303     return E_NOINTERFACE;
304 }
305
306 static ULONG STDMETHODCALLTYPE d3d10_texture3d_AddRef(ID3D10Texture3D *iface)
307 {
308     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
309     ULONG refcount = InterlockedIncrement(&texture->refcount);
310
311     TRACE("%p increasing refcount to %u.\n", texture, refcount);
312
313     if (refcount == 1)
314         wined3d_texture_incref(texture->wined3d_texture);
315
316     return refcount;
317 }
318
319 static void STDMETHODCALLTYPE d3d10_texture3d_wined3d_object_released(void *parent)
320 {
321     HeapFree(GetProcessHeap(), 0, parent);
322 }
323
324 static ULONG STDMETHODCALLTYPE d3d10_texture3d_Release(ID3D10Texture3D *iface)
325 {
326     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
327     ULONG refcount = InterlockedDecrement(&texture->refcount);
328
329     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
330
331     if (!refcount)
332         wined3d_texture_decref(texture->wined3d_texture);
333
334     return refcount;
335 }
336
337 static void STDMETHODCALLTYPE d3d10_texture3d_GetDevice(ID3D10Texture3D *iface, ID3D10Device **device)
338 {
339     FIXME("iface %p, device %p stub!\n", iface, device);
340 }
341
342 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_GetPrivateData(ID3D10Texture3D *iface,
343         REFGUID guid, UINT *data_size, void *data)
344 {
345     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
346             iface, debugstr_guid(guid), data_size, data);
347
348     return E_NOTIMPL;
349 }
350
351 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateData(ID3D10Texture3D *iface,
352         REFGUID guid, UINT data_size, const void *data)
353 {
354     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
355             iface, debugstr_guid(guid), data_size, data);
356
357     return E_NOTIMPL;
358 }
359
360 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_SetPrivateDataInterface(ID3D10Texture3D *iface,
361         REFGUID guid, const IUnknown *data)
362 {
363     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
364
365     return E_NOTIMPL;
366 }
367
368 static void STDMETHODCALLTYPE d3d10_texture3d_GetType(ID3D10Texture3D *iface,
369         D3D10_RESOURCE_DIMENSION *resource_dimension)
370 {
371     TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
372
373     *resource_dimension = D3D10_RESOURCE_DIMENSION_TEXTURE3D;
374 }
375
376 static void STDMETHODCALLTYPE d3d10_texture3d_SetEvictionPriority(ID3D10Texture3D *iface, UINT eviction_priority)
377 {
378     FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
379 }
380
381 static UINT STDMETHODCALLTYPE d3d10_texture3d_GetEvictionPriority(ID3D10Texture3D *iface)
382 {
383     FIXME("iface %p stub!\n", iface);
384
385     return 0;
386 }
387
388 static HRESULT STDMETHODCALLTYPE d3d10_texture3d_Map(ID3D10Texture3D *iface, UINT sub_resource_idx,
389         D3D10_MAP map_type, UINT map_flags, D3D10_MAPPED_TEXTURE3D *mapped_texture)
390 {
391     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
392     struct wined3d_map_desc wined3d_map_desc;
393     struct wined3d_resource *sub_resource;
394     HRESULT hr;
395
396     TRACE("iface %p, sub_resource_idx %u, map_type %u, map_flags %#x, mapped_texture %p.\n",
397             iface, sub_resource_idx, map_type, map_flags, mapped_texture);
398
399     if (map_type != D3D10_MAP_READ_WRITE)
400         FIXME("Ignoring map_type %#x.\n", map_type);
401     if (map_flags)
402         FIXME("Ignoring map_flags %#x.\n", map_flags);
403
404     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
405         hr = E_INVALIDARG;
406     else if (SUCCEEDED(hr = wined3d_volume_map(wined3d_volume_from_resource(sub_resource),
407             &wined3d_map_desc, NULL, 0)))
408     {
409         mapped_texture->pData = wined3d_map_desc.data;
410         mapped_texture->RowPitch = wined3d_map_desc.row_pitch;
411         mapped_texture->DepthPitch = wined3d_map_desc.slice_pitch;
412     }
413
414     return hr;
415 }
416
417 static void STDMETHODCALLTYPE d3d10_texture3d_Unmap(ID3D10Texture3D *iface, UINT sub_resource_idx)
418 {
419     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
420     struct wined3d_resource *sub_resource;
421
422     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
423
424     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
425         return;
426
427     wined3d_volume_unmap(wined3d_volume_from_resource(sub_resource));
428 }
429
430 static void STDMETHODCALLTYPE d3d10_texture3d_GetDesc(ID3D10Texture3D *iface, D3D10_TEXTURE3D_DESC *desc)
431 {
432     struct d3d10_texture3d *texture = impl_from_ID3D10Texture3D(iface);
433
434     TRACE("iface %p, desc %p.\n", iface, desc);
435
436     *desc = texture->desc;
437 }
438
439 static const struct ID3D10Texture3DVtbl d3d10_texture3d_vtbl =
440 {
441     /* IUnknown methods */
442     d3d10_texture3d_QueryInterface,
443     d3d10_texture3d_AddRef,
444     d3d10_texture3d_Release,
445     /* ID3D10DeviceChild methods */
446     d3d10_texture3d_GetDevice,
447     d3d10_texture3d_GetPrivateData,
448     d3d10_texture3d_SetPrivateData,
449     d3d10_texture3d_SetPrivateDataInterface,
450     /* ID3D10Resource methods */
451     d3d10_texture3d_GetType,
452     d3d10_texture3d_SetEvictionPriority,
453     d3d10_texture3d_GetEvictionPriority,
454     /* ID3D10Texture3D methods */
455     d3d10_texture3d_Map,
456     d3d10_texture3d_Unmap,
457     d3d10_texture3d_GetDesc,
458 };
459
460 static const struct wined3d_parent_ops d3d10_texture3d_wined3d_parent_ops =
461 {
462     d3d10_texture3d_wined3d_object_released,
463 };
464
465 HRESULT d3d10_texture3d_init(struct d3d10_texture3d *texture, struct d3d10_device *device,
466         const D3D10_TEXTURE3D_DESC *desc)
467 {
468     HRESULT hr;
469
470     texture->ID3D10Texture3D_iface.lpVtbl = &d3d10_texture3d_vtbl;
471     texture->refcount = 1;
472     texture->desc = *desc;
473
474     FIXME("Implement DXGI<->wined3d usage conversion.\n");
475
476     hr = wined3d_texture_create_3d(device->wined3d_device, desc->Width, desc->Height, desc->Depth,
477             desc->MipLevels, desc->Usage, wined3dformat_from_dxgi_format(desc->Format), WINED3D_POOL_DEFAULT,
478             texture, &d3d10_texture3d_wined3d_parent_ops, &texture->wined3d_texture);
479     if (FAILED(hr))
480     {
481         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
482         return hr;
483     }
484     texture->desc.MipLevels = wined3d_texture_get_level_count(texture->wined3d_texture);
485
486     return S_OK;
487 }