wined3d: Remove COM from IWineD3D.
[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         IWineD3DDevice_Release(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 = IWineD3DDevice_GetCreationParameters(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     IWineD3DDeviceParent *device_parent;
162     HRESULT hr;
163     UINT i;
164     UINT j;
165
166     TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
167             iface, desc, surface_count, usage, shared_resource, surface);
168
169     hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineD3DDeviceParent, (void **)&device_parent);
170     if (FAILED(hr))
171     {
172         ERR("Device should implement IWineD3DDeviceParent\n");
173         return E_FAIL;
174     }
175
176     FIXME("Implement DXGI<->wined3d usage conversion\n");
177
178     memset(surface, 0, surface_count * sizeof(*surface));
179     for (i = 0; i < surface_count; ++i)
180     {
181         IWineD3DSurface *wined3d_surface;
182         IUnknown *parent;
183
184         hr = IWineD3DDeviceParent_CreateSurface(device_parent, NULL, desc->Width, desc->Height,
185                 wined3dformat_from_dxgi_format(desc->Format), usage, WINED3DPOOL_DEFAULT, 0,
186                 WINED3DCUBEMAP_FACE_POSITIVE_X, &wined3d_surface);
187         if (FAILED(hr))
188         {
189             ERR("CreateSurface failed, returning %#x\n", hr);
190             goto fail;
191         }
192
193         parent = IWineD3DSurface_GetParent(wined3d_surface);
194         hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
195         IWineD3DSurface_Release(wined3d_surface);
196         if (FAILED(hr))
197         {
198             ERR("Surface should implement IDXGISurface\n");
199             goto fail;
200         }
201
202         TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
203     }
204     IWineD3DDeviceParent_Release(device_parent);
205
206     return S_OK;
207
208 fail:
209     for (j = 0; j < i; ++j)
210     {
211         IDXGISurface_Release(surface[i]);
212     }
213     IWineD3DDeviceParent_Release(device_parent);
214     return hr;
215 }
216
217 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
218         IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
219 {
220     FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
221             iface, resources, residency, resource_count);
222
223     return E_NOTIMPL;
224 }
225
226 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
227 {
228     FIXME("iface %p, priority %d stub!\n", iface, priority);
229
230     return E_NOTIMPL;
231 }
232
233 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
234 {
235     FIXME("iface %p, priority %p stub!\n", iface, priority);
236
237     return E_NOTIMPL;
238 }
239
240 /* IWineDXGIDevice methods */
241
242 static IWineD3DDevice * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
243 {
244     struct dxgi_device *This = (struct dxgi_device *)iface;
245
246     TRACE("iface %p\n", iface);
247
248     EnterCriticalSection(&dxgi_cs);
249     IWineD3DDevice_AddRef(This->wined3d_device);
250     LeaveCriticalSection(&dxgi_cs);
251     return This->wined3d_device;
252 }
253
254 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
255         DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
256 {
257     struct dxgi_surface *object;
258     HRESULT hr;
259
260     FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
261             iface, desc, usage, shared_resource, outer, surface);
262
263     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
264     if (!object)
265     {
266         ERR("Failed to allocate DXGI surface object memory\n");
267         return E_OUTOFMEMORY;
268     }
269
270     hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer);
271     if (FAILED(hr))
272     {
273         WARN("Failed to initialize surface, hr %#x.\n", hr);
274         HeapFree(GetProcessHeap(), 0, object);
275         return hr;
276     }
277
278     TRACE("Created IDXGISurface %p\n", object);
279     *surface = outer ? (void *)&object->inner_unknown_vtbl : object;
280
281     return S_OK;
282 }
283
284 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
285         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **wined3d_swapchain)
286 {
287     struct dxgi_swapchain *object;
288     HRESULT hr;
289
290     TRACE("iface %p, present_parameters %p, wined3d_swapchain %p.\n",
291             iface, present_parameters, wined3d_swapchain);
292
293     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
294     if (!object)
295     {
296         ERR("Failed to allocate DXGI swapchain object memory\n");
297         return E_OUTOFMEMORY;
298     }
299
300     hr = dxgi_swapchain_init(object, (struct dxgi_device *)iface, present_parameters);
301     if (FAILED(hr))
302     {
303         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
304         HeapFree(GetProcessHeap(), 0, object);
305         return hr;
306     }
307
308     TRACE("Created IDXGISwapChain %p\n", object);
309     *wined3d_swapchain = object->wined3d_swapchain;
310
311     return S_OK;
312 }
313
314 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
315 {
316     /* IUnknown methods */
317     dxgi_device_QueryInterface,
318     dxgi_device_AddRef,
319     dxgi_device_Release,
320     /* IDXGIObject methods */
321     dxgi_device_SetPrivateData,
322     dxgi_device_SetPrivateDataInterface,
323     dxgi_device_GetPrivateData,
324     dxgi_device_GetParent,
325     /* IDXGIDevice methods */
326     dxgi_device_GetAdapter,
327     dxgi_device_CreateSurface,
328     dxgi_device_QueryResourceResidency,
329     dxgi_device_SetGPUThreadPriority,
330     dxgi_device_GetGPUThreadPriority,
331     /* IWineDXGIAdapter methods */
332     dxgi_device_get_wined3d_device,
333     dxgi_device_create_surface,
334     dxgi_device_create_swapchain,
335 };
336
337 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
338         IDXGIFactory *factory, IDXGIAdapter *adapter)
339 {
340     IWineD3DDeviceParent *wined3d_device_parent;
341     IWineDXGIAdapter *wine_adapter;
342     UINT adapter_ordinal;
343     struct wined3d *wined3d;
344     void *layer_base;
345     HRESULT hr;
346
347     device->vtbl = &dxgi_device_vtbl;
348     device->refcount = 1;
349
350     layer_base = device + 1;
351
352     hr = layer->create(layer->id, &layer_base, 0,
353             device, &IID_IUnknown, (void **)&device->child_layer);
354     if (FAILED(hr))
355     {
356         WARN("Failed to create device, returning %#x.\n", hr);
357         goto fail;
358     }
359
360     hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
361     if (FAILED(hr))
362     {
363         WARN("This is not the factory we're looking for, returning %#x.\n", hr);
364         goto fail;
365     }
366     wined3d = IWineDXGIFactory_get_wined3d(device->factory);
367
368     hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
369     if (FAILED(hr))
370     {
371         WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
372         EnterCriticalSection(&dxgi_cs);
373         wined3d_decref(wined3d);
374         LeaveCriticalSection(&dxgi_cs);
375         goto fail;
376     }
377     adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
378     IWineDXGIAdapter_Release(wine_adapter);
379
380     hr = IUnknown_QueryInterface((IUnknown *)device, &IID_IWineD3DDeviceParent, (void **)&wined3d_device_parent);
381     if (FAILED(hr))
382     {
383         ERR("DXGI device should implement IWineD3DDeviceParent.\n");
384         goto fail;
385     }
386
387     FIXME("Ignoring adapter type.\n");
388     EnterCriticalSection(&dxgi_cs);
389     hr = wined3d_device_create(wined3d, adapter_ordinal, WINED3DDEVTYPE_HAL, NULL, 0,
390             wined3d_device_parent, &device->wined3d_device);
391     IWineD3DDeviceParent_Release(wined3d_device_parent);
392     wined3d_decref(wined3d);
393     LeaveCriticalSection(&dxgi_cs);
394     if (FAILED(hr))
395     {
396         WARN("Failed to create a wined3d device, returning %#x.\n", hr);
397         goto fail;
398     }
399
400     return S_OK;
401
402 fail:
403     if (device->wined3d_device)
404     {
405         EnterCriticalSection(&dxgi_cs);
406         IWineD3DDevice_Release(device->wined3d_device);
407         LeaveCriticalSection(&dxgi_cs);
408     }
409     if (device->factory) IWineDXGIFactory_Release(device->factory);
410     if (device->child_layer) IUnknown_Release(device->child_layer);
411     return hr;
412 }