d3d10: COM cleanup for the ID3D10EffectType iface.
[wine] / dlls / dxgi / device.c
1 /*
2  * Copyright 2008 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 "dxgi_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
26
27 /* IUnknown methods */
28
29 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
30 {
31     struct dxgi_device *This = (struct dxgi_device *)iface;
32
33     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
34
35     if (IsEqualGUID(riid, &IID_IUnknown)
36             || IsEqualGUID(riid, &IID_IDXGIObject)
37             || IsEqualGUID(riid, &IID_IDXGIDevice)
38             || IsEqualGUID(riid, &IID_IWineDXGIDevice))
39     {
40         IUnknown_AddRef(iface);
41         *object = iface;
42         return S_OK;
43     }
44
45     if (This->child_layer)
46     {
47         TRACE("forwarding to child layer %p\n", This->child_layer);
48         return IUnknown_QueryInterface(This->child_layer, riid, object);
49     }
50
51     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
52
53     *object = NULL;
54     return E_NOINTERFACE;
55 }
56
57 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
58 {
59     struct dxgi_device *This = (struct dxgi_device *)iface;
60     ULONG refcount = InterlockedIncrement(&This->refcount);
61
62     TRACE("%p increasing refcount to %u\n", This, refcount);
63
64     return refcount;
65 }
66
67 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
68 {
69     struct dxgi_device *This = (struct dxgi_device *)iface;
70     ULONG refcount = InterlockedDecrement(&This->refcount);
71
72     TRACE("%p decreasing refcount to %u\n", This, refcount);
73
74     if (!refcount)
75     {
76         if (This->child_layer) IUnknown_Release(This->child_layer);
77         EnterCriticalSection(&dxgi_cs);
78         wined3d_device_decref(This->wined3d_device);
79         LeaveCriticalSection(&dxgi_cs);
80         IWineDXGIFactory_Release(This->factory);
81         HeapFree(GetProcessHeap(), 0, This);
82     }
83
84     return refcount;
85 }
86
87 /* IDXGIObject methods */
88
89 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
90         REFGUID guid, UINT data_size, const void *data)
91 {
92     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
93
94     return E_NOTIMPL;
95 }
96
97 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
98         REFGUID guid, const IUnknown *object)
99 {
100     FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
101
102     return E_NOTIMPL;
103 }
104
105 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
106         REFGUID guid, UINT *data_size, void *data)
107 {
108     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
109
110     return E_NOTIMPL;
111 }
112
113 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
114 {
115     IDXGIAdapter *adapter;
116     HRESULT hr;
117
118     TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
119
120     hr = IDXGIDevice_GetAdapter(iface, &adapter);
121     if (FAILED(hr))
122     {
123         ERR("Failed to get adapter, hr %#x.\n", hr);
124         return hr;
125     }
126
127     hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
128     IDXGIAdapter_Release(adapter);
129
130     return hr;
131 }
132
133 /* IDXGIDevice methods */
134
135 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
136 {
137     struct dxgi_device *This = (struct dxgi_device *)iface;
138     WINED3DDEVICE_CREATION_PARAMETERS create_parameters;
139     HRESULT hr;
140
141     TRACE("iface %p, adapter %p\n", iface, adapter);
142
143     EnterCriticalSection(&dxgi_cs);
144
145     hr = wined3d_device_get_creation_parameters(This->wined3d_device, &create_parameters);
146     if (FAILED(hr))
147     {
148         LeaveCriticalSection(&dxgi_cs);
149         return hr;
150     }
151
152     LeaveCriticalSection(&dxgi_cs);
153
154     return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.AdapterOrdinal, adapter);
155 }
156
157 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
158         const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
159         const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
160 {
161     struct wined3d_device_parent *device_parent;
162     IWineDXGIDeviceParent *dxgi_device_parent;
163     HRESULT hr;
164     UINT i;
165     UINT j;
166
167     TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
168             iface, desc, surface_count, usage, shared_resource, surface);
169
170     hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
171     if (FAILED(hr))
172     {
173         ERR("Device should implement IWineD3DDeviceParent\n");
174         return E_FAIL;
175     }
176
177     device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
178
179     FIXME("Implement DXGI<->wined3d usage conversion\n");
180
181     memset(surface, 0, surface_count * sizeof(*surface));
182     for (i = 0; i < surface_count; ++i)
183     {
184         struct wined3d_surface *wined3d_surface;
185         IUnknown *parent;
186
187         hr = device_parent->ops->create_surface(device_parent, NULL, desc->Width, desc->Height,
188                 wined3dformat_from_dxgi_format(desc->Format), usage, WINED3DPOOL_DEFAULT, 0,
189                 WINED3DCUBEMAP_FACE_POSITIVE_X, &wined3d_surface);
190         if (FAILED(hr))
191         {
192             ERR("CreateSurface failed, returning %#x\n", hr);
193             goto fail;
194         }
195
196         parent = wined3d_surface_get_parent(wined3d_surface);
197         hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
198         wined3d_surface_decref(wined3d_surface);
199         if (FAILED(hr))
200         {
201             ERR("Surface should implement IDXGISurface\n");
202             goto fail;
203         }
204
205         TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
206     }
207     IWineDXGIDeviceParent_Release(dxgi_device_parent);
208
209     return S_OK;
210
211 fail:
212     for (j = 0; j < i; ++j)
213     {
214         IDXGISurface_Release(surface[i]);
215     }
216     IWineDXGIDeviceParent_Release(dxgi_device_parent);
217     return hr;
218 }
219
220 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
221         IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
222 {
223     FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
224             iface, resources, residency, resource_count);
225
226     return E_NOTIMPL;
227 }
228
229 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
230 {
231     FIXME("iface %p, priority %d stub!\n", iface, priority);
232
233     return E_NOTIMPL;
234 }
235
236 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
237 {
238     FIXME("iface %p, priority %p stub!\n", iface, priority);
239
240     return E_NOTIMPL;
241 }
242
243 /* IWineDXGIDevice methods */
244
245 static struct wined3d_device * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
246 {
247     struct dxgi_device *This = (struct dxgi_device *)iface;
248
249     TRACE("iface %p\n", iface);
250
251     EnterCriticalSection(&dxgi_cs);
252     wined3d_device_incref(This->wined3d_device);
253     LeaveCriticalSection(&dxgi_cs);
254     return This->wined3d_device;
255 }
256
257 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
258         DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
259 {
260     struct dxgi_surface *object;
261     HRESULT hr;
262
263     FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
264             iface, desc, usage, shared_resource, outer, surface);
265
266     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
267     if (!object)
268     {
269         ERR("Failed to allocate DXGI surface object memory\n");
270         return E_OUTOFMEMORY;
271     }
272
273     hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer);
274     if (FAILED(hr))
275     {
276         WARN("Failed to initialize surface, hr %#x.\n", hr);
277         HeapFree(GetProcessHeap(), 0, object);
278         return hr;
279     }
280
281     TRACE("Created IDXGISurface %p\n", object);
282     *surface = outer ? (void *)&object->inner_unknown_vtbl : object;
283
284     return S_OK;
285 }
286
287 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
288         WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **wined3d_swapchain)
289 {
290     struct dxgi_swapchain *object;
291     HRESULT hr;
292
293     TRACE("iface %p, present_parameters %p, wined3d_swapchain %p.\n",
294             iface, present_parameters, wined3d_swapchain);
295
296     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
297     if (!object)
298     {
299         ERR("Failed to allocate DXGI swapchain object memory\n");
300         return E_OUTOFMEMORY;
301     }
302
303     hr = dxgi_swapchain_init(object, (struct dxgi_device *)iface, present_parameters);
304     if (FAILED(hr))
305     {
306         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
307         HeapFree(GetProcessHeap(), 0, object);
308         return hr;
309     }
310
311     TRACE("Created IDXGISwapChain %p\n", object);
312     *wined3d_swapchain = object->wined3d_swapchain;
313
314     return S_OK;
315 }
316
317 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
318 {
319     /* IUnknown methods */
320     dxgi_device_QueryInterface,
321     dxgi_device_AddRef,
322     dxgi_device_Release,
323     /* IDXGIObject methods */
324     dxgi_device_SetPrivateData,
325     dxgi_device_SetPrivateDataInterface,
326     dxgi_device_GetPrivateData,
327     dxgi_device_GetParent,
328     /* IDXGIDevice methods */
329     dxgi_device_GetAdapter,
330     dxgi_device_CreateSurface,
331     dxgi_device_QueryResourceResidency,
332     dxgi_device_SetGPUThreadPriority,
333     dxgi_device_GetGPUThreadPriority,
334     /* IWineDXGIAdapter methods */
335     dxgi_device_get_wined3d_device,
336     dxgi_device_create_surface,
337     dxgi_device_create_swapchain,
338 };
339
340 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
341         IDXGIFactory *factory, IDXGIAdapter *adapter)
342 {
343     struct wined3d_device_parent *wined3d_device_parent;
344     IWineDXGIDeviceParent *dxgi_device_parent;
345     IWineDXGIAdapter *wine_adapter;
346     UINT adapter_ordinal;
347     struct wined3d *wined3d;
348     void *layer_base;
349     HRESULT hr;
350
351     device->vtbl = &dxgi_device_vtbl;
352     device->refcount = 1;
353
354     layer_base = device + 1;
355
356     hr = layer->create(layer->id, &layer_base, 0,
357             device, &IID_IUnknown, (void **)&device->child_layer);
358     if (FAILED(hr))
359     {
360         WARN("Failed to create device, returning %#x.\n", hr);
361         goto fail;
362     }
363
364     hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
365     if (FAILED(hr))
366     {
367         WARN("This is not the factory we're looking for, returning %#x.\n", hr);
368         goto fail;
369     }
370     wined3d = IWineDXGIFactory_get_wined3d(device->factory);
371
372     hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
373     if (FAILED(hr))
374     {
375         WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
376         EnterCriticalSection(&dxgi_cs);
377         wined3d_decref(wined3d);
378         LeaveCriticalSection(&dxgi_cs);
379         goto fail;
380     }
381     adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
382     IWineDXGIAdapter_Release(wine_adapter);
383
384     hr = IUnknown_QueryInterface((IUnknown *)device, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
385     if (FAILED(hr))
386     {
387         ERR("DXGI device should implement IWineD3DDeviceParent.\n");
388         goto fail;
389     }
390
391     wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
392
393     FIXME("Ignoring adapter type.\n");
394     EnterCriticalSection(&dxgi_cs);
395     hr = wined3d_device_create(wined3d, adapter_ordinal, WINED3DDEVTYPE_HAL, NULL, 0,
396             wined3d_device_parent, &device->wined3d_device);
397     IWineDXGIDeviceParent_Release(dxgi_device_parent);
398     wined3d_decref(wined3d);
399     LeaveCriticalSection(&dxgi_cs);
400     if (FAILED(hr))
401     {
402         WARN("Failed to create a wined3d device, returning %#x.\n", hr);
403         goto fail;
404     }
405
406     return S_OK;
407
408 fail:
409     if (device->wined3d_device)
410     {
411         EnterCriticalSection(&dxgi_cs);
412         wined3d_device_decref(device->wined3d_device);
413         LeaveCriticalSection(&dxgi_cs);
414     }
415     if (device->factory) IWineDXGIFactory_Release(device->factory);
416     if (device->child_layer) IUnknown_Release(device->child_layer);
417     return hr;
418 }