dbghelp: Use CONTAINING_RECORD instead of reimplementing it.
[wine.git] / dlls / dxgi / adapter.c
blob3e58fd2c278108c2e9514c8059ccd51b10fbfe91
1 /*
2 * Copyright 2008 Henri Verbeet for CodeWeavers
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.
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.
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include "dxgi_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
27 static inline struct dxgi_adapter *impl_from_IDXGIAdapter1(IDXGIAdapter1 *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_adapter, IDXGIAdapter1_iface);
32 static HRESULT STDMETHODCALLTYPE dxgi_adapter_QueryInterface(IDXGIAdapter1 *iface, REFIID iid, void **out)
34 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
36 if (IsEqualGUID(iid, &IID_IDXGIAdapter1)
37 || IsEqualGUID(iid, &IID_IDXGIAdapter)
38 || IsEqualGUID(iid, &IID_IDXGIObject)
39 || IsEqualGUID(iid, &IID_IUnknown))
41 IUnknown_AddRef(iface);
42 *out = iface;
43 return S_OK;
46 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
48 *out = NULL;
49 return E_NOINTERFACE;
52 static ULONG STDMETHODCALLTYPE dxgi_adapter_AddRef(IDXGIAdapter1 *iface)
54 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
55 ULONG refcount = InterlockedIncrement(&adapter->refcount);
57 TRACE("%p increasing refcount to %u.\n", iface, refcount);
59 return refcount;
62 static ULONG STDMETHODCALLTYPE dxgi_adapter_Release(IDXGIAdapter1 *iface)
64 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
65 ULONG refcount = InterlockedDecrement(&adapter->refcount);
67 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
69 if (!refcount)
71 IDXGIOutput_Release(adapter->output);
72 wined3d_private_store_cleanup(&adapter->private_store);
73 HeapFree(GetProcessHeap(), 0, adapter);
76 return refcount;
79 static HRESULT STDMETHODCALLTYPE dxgi_adapter_SetPrivateData(IDXGIAdapter1 *iface,
80 REFGUID guid, UINT data_size, const void *data)
82 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
84 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
86 return dxgi_set_private_data(&adapter->private_store, guid, data_size, data);
89 static HRESULT STDMETHODCALLTYPE dxgi_adapter_SetPrivateDataInterface(IDXGIAdapter1 *iface,
90 REFGUID guid, const IUnknown *object)
92 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
94 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
96 return dxgi_set_private_data_interface(&adapter->private_store, guid, object);
99 static HRESULT STDMETHODCALLTYPE dxgi_adapter_GetPrivateData(IDXGIAdapter1 *iface,
100 REFGUID guid, UINT *data_size, void *data)
102 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
104 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
106 return dxgi_get_private_data(&adapter->private_store, guid, data_size, data);
109 static HRESULT STDMETHODCALLTYPE dxgi_adapter_GetParent(IDXGIAdapter1 *iface, REFIID iid, void **parent)
111 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
113 TRACE("iface %p, iid %s, parent %p\n", iface, debugstr_guid(iid), parent);
115 return IDXGIFactory1_QueryInterface(&adapter->parent->IDXGIFactory1_iface, iid, parent);
118 static HRESULT STDMETHODCALLTYPE dxgi_adapter_EnumOutputs(IDXGIAdapter1 *iface,
119 UINT output_idx, IDXGIOutput **output)
121 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
123 TRACE("iface %p, output_idx %u, output %p.\n", iface, output_idx, output);
125 if (output_idx > 0)
127 *output = NULL;
128 return DXGI_ERROR_NOT_FOUND;
131 *output = adapter->output;
132 IDXGIOutput_AddRef(*output);
134 TRACE("Returning output %p.\n", output);
136 return S_OK;
139 static HRESULT STDMETHODCALLTYPE dxgi_adapter_GetDesc1(IDXGIAdapter1 *iface, DXGI_ADAPTER_DESC1 *desc)
141 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
142 struct wined3d_adapter_identifier adapter_id;
143 char description[128];
144 HRESULT hr;
146 TRACE("iface %p, desc %p.\n", iface, desc);
148 if (!desc)
149 return E_INVALIDARG;
151 adapter_id.driver_size = 0;
152 adapter_id.description = description;
153 adapter_id.description_size = sizeof(description);
154 adapter_id.device_name_size = 0;
156 wined3d_mutex_lock();
157 hr = wined3d_get_adapter_identifier(adapter->parent->wined3d, adapter->ordinal, 0, &adapter_id);
158 wined3d_mutex_unlock();
160 if (FAILED(hr))
161 return hr;
163 if (!MultiByteToWideChar(CP_ACP, 0, description, -1, desc->Description, 128))
165 DWORD err = GetLastError();
166 ERR("Failed to translate description %s (%#x).\n", debugstr_a(description), err);
167 hr = E_FAIL;
170 desc->VendorId = adapter_id.vendor_id;
171 desc->DeviceId = adapter_id.device_id;
172 desc->SubSysId = adapter_id.subsystem_id;
173 desc->Revision = adapter_id.revision;
174 desc->DedicatedVideoMemory = adapter_id.video_memory;
175 desc->DedicatedSystemMemory = 0; /* FIXME */
176 desc->SharedSystemMemory = 0; /* FIXME */
177 memcpy(&desc->AdapterLuid, &adapter_id.adapter_luid, sizeof(desc->AdapterLuid));
178 desc->Flags = 0;
180 return hr;
183 static HRESULT STDMETHODCALLTYPE dxgi_adapter_GetDesc(IDXGIAdapter1 *iface, DXGI_ADAPTER_DESC *desc)
185 DXGI_ADAPTER_DESC1 desc1;
186 HRESULT hr;
188 TRACE("iface %p, desc %p.\n", iface, desc);
190 if (!desc)
191 return E_INVALIDARG;
193 if (FAILED(hr = dxgi_adapter_GetDesc1(iface, &desc1)))
194 return hr;
195 memcpy(desc, &desc1, sizeof(*desc));
197 return hr;
200 static HRESULT STDMETHODCALLTYPE dxgi_adapter_CheckInterfaceSupport(IDXGIAdapter1 *iface,
201 REFGUID guid, LARGE_INTEGER *umd_version)
203 struct dxgi_adapter *adapter = impl_from_IDXGIAdapter1(iface);
204 struct wined3d_adapter_identifier adapter_id;
205 HRESULT hr;
207 TRACE("iface %p, guid %s, umd_version %p.\n", iface, debugstr_guid(guid), umd_version);
209 /* This method works only for D3D10 interfaces. */
210 if (!(IsEqualGUID(guid, &IID_ID3D10Device)
211 || IsEqualGUID(guid, &IID_ID3D10Device1)))
213 WARN("Returning DXGI_ERROR_UNSUPPORTED for %s.\n", debugstr_guid(guid));
214 return DXGI_ERROR_UNSUPPORTED;
217 if (FAILED(hr = dxgi_check_d3d10_support(adapter->parent, adapter)))
218 return DXGI_ERROR_UNSUPPORTED;
220 if (umd_version)
222 adapter_id.driver_size = 0;
223 adapter_id.description_size = 0;
224 adapter_id.device_name_size = 0;
226 wined3d_mutex_lock();
227 hr = wined3d_get_adapter_identifier(adapter->parent->wined3d, adapter->ordinal, 0, &adapter_id);
228 wined3d_mutex_unlock();
229 if (FAILED(hr))
230 return hr;
232 *umd_version = adapter_id.driver_version;
235 return S_OK;
238 static const struct IDXGIAdapter1Vtbl dxgi_adapter_vtbl =
240 dxgi_adapter_QueryInterface,
241 dxgi_adapter_AddRef,
242 dxgi_adapter_Release,
243 dxgi_adapter_SetPrivateData,
244 dxgi_adapter_SetPrivateDataInterface,
245 dxgi_adapter_GetPrivateData,
246 dxgi_adapter_GetParent,
247 dxgi_adapter_EnumOutputs,
248 dxgi_adapter_GetDesc,
249 dxgi_adapter_CheckInterfaceSupport,
250 dxgi_adapter_GetDesc1,
253 struct dxgi_adapter *unsafe_impl_from_IDXGIAdapter1(IDXGIAdapter1 *iface)
255 if (!iface)
256 return NULL;
257 assert(iface->lpVtbl == &dxgi_adapter_vtbl);
258 return CONTAINING_RECORD(iface, struct dxgi_adapter, IDXGIAdapter1_iface);
261 HRESULT dxgi_adapter_init(struct dxgi_adapter *adapter, struct dxgi_factory *parent, UINT ordinal)
263 struct dxgi_output *output;
265 adapter->IDXGIAdapter1_iface.lpVtbl = &dxgi_adapter_vtbl;
266 adapter->parent = parent;
267 adapter->refcount = 1;
268 wined3d_private_store_init(&adapter->private_store);
269 adapter->ordinal = ordinal;
271 if (!(output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*output))))
273 wined3d_private_store_cleanup(&adapter->private_store);
274 return E_OUTOFMEMORY;
276 dxgi_output_init(output, adapter);
277 adapter->output = &output->IDXGIOutput_iface;
279 return S_OK;